note 126480 added to domdocument.loadhtml

From: Date: Tue, 05 Oct 2021 15:21:16 +0000
Subject: note 126480 added to domdocument.loadhtml
Groups: php.notes 
Request: Send a blank email to php-notes+get-217033@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: _______________ $dom=new DOMDocument('1.0','utf-8'); libxml_use_internal_errors(true); $dom->loadHTMLFile("template.php"); echo $dom->saveHTML(); flush(); ob_flush(); _______________ This needs to be a part of the Docu since you want to know this in the beginning when you structure the project because yoe 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: _______________ $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.loadhtml.php Edit -- https://main.php.net/note/edit/126480 Del: integrated -- https://main.php.net/note/delete/126480/integrated Del: useless -- https://main.php.net/note/delete/126480/useless Del: bad code -- https://main.php.net/note/delete/126480/bad+code Del: spam -- https://main.php.net/note/delete/126480/spam Del: non-english -- https://main.php.net/note/delete/126480/non-english Del: in docs -- https://main.php.net/note/delete/126480/in+docs Del: other reasons-- https://main.php.net/note/delete/126480 Reject -- https://main.php.net/note/reject/126480 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217033) next »