note 126482 added to domdocument.loadhtmlfile

From: Date: Tue, 05 Oct 2021 22:48:51 +0000
Subject: note 126482 added to domdocument.loadhtmlfile
Groups: php.notes 
Request: Send a blank email to php-notes+get-217035@lists.php.net to get a copy of this message
If you use a template in which you pass a variable from php to Javascript and embed the template via loadHTMLFile, you may experience difficulties. In the following example template.php is interpreted just fine on its own, but when embedded in the index.php it throws the syntaxError unexpected token '<' and the ReferenceError that par is not defined in the onload statement. template.php: _______________ <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <script type="text/javascript"> var testvar=<?php echo "'blub'"; ?> var par =document.createElement("p"); var text=document.createTextNode(testvar); par.append(text); </script> </head> <body onload="document.body.append(par);"> can you see a blub after this text: <noscript> probably not</noscript> </body> </html> _______________ index.php: _______________ <?php $dom=new DOMDocument('1.0','utf-8'); libxml_use_internal_errors(true); $dom->loadHTMLFile("template.php"); echo $dom->saveHTML(); flush(); ob_flush(); ?> _______________ you want to know this in the beginning when you read the docu and structure the project because you can actually work around this: The workaround for this problem is to not transfer the variable testvar from php to javascript in the template but in the index.php: template.php: _______________ <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> </head> <body onload="document.body.append(par);"> can you see a blub after this text: <noscript> probably not</noscript> </body> </html> _______________ index.php: _______________ <?php $dom=new DOMDocument('1.0','utf-8'); libxml_use_internal_errors(true); $dom->loadHTMLFile("template.php"); //new: $testvar="blub"; $js=$dom->createTextNode('script'); $js->setAttribute('type','text/javascript'); $text=$dom->createTextNode(' var testvar= "'.blub.'"; var par =document.createElement("p"); var text=document.createTextNode(testvar); par.append(text);'); $js->appendChild($text); $dom->getElementsByTagName('head')[0]->appendChild($js); echo $dom->saveHTML(); flush(); ob_flush(); ?> _______________ ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 84.185.190.107) ---- Manual Page -- https://php.net/manual/en/domdocument.loadhtmlfile.php Edit -- https://main.php.net/note/edit/126482 Del: integrated -- https://main.php.net/note/delete/126482/integrated Del: useless -- https://main.php.net/note/delete/126482/useless Del: bad code -- https://main.php.net/note/delete/126482/bad+code Del: spam -- https://main.php.net/note/delete/126482/spam Del: non-english -- https://main.php.net/note/delete/126482/non-english Del: in docs -- https://main.php.net/note/delete/126482/in+docs Del: other reasons-- https://main.php.net/note/delete/126482 Reject -- https://main.php.net/note/reject/126482 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217035) next »