Rectangle 27 0

php How to use DOMDocument to add script to HTML5?


For reference, adding a completely empty text node does the job, it doesn't even need to have a space in it.

Javascript tags, even if they're loading an external file via the src= attribute, can't be self closing. You may need to add some non-empty content to the DOM element you're creating to force it to be non-self closing. A textnode with a single space would do.

OK changing $script = $document->createElement ( 'script', '' ); to $script = $document->createElement ( 'script', ' ' ); produces <script src="script.js"> </script> Thanks for that. Now how can I get DOmDocument to produce <script src="script.js"></script> i.e. without the extra space?

That I don't know. Wonder if it does that for all tags, or are JS tags buggy? Try creating a <p> with no content and see if that self closes.

Note
Rectangle 27 0

php How to use DOMDocument to add script to HTML5?


For reference, adding a completely empty text node does the job, it doesn't even need to have a space in it.

Javascript tags, even if they're loading an external file via the src= attribute, can't be self closing. You may need to add some non-empty content to the DOM element you're creating to force it to be non-self closing. A textnode with a single space would do.

OK changing $script = $document->createElement ( 'script', '' ); to $script = $document->createElement ( 'script', ' ' ); produces <script src="script.js"> </script> Thanks for that. Now how can I get DOmDocument to produce <script src="script.js"></script> i.e. without the extra space?

That I don't know. Wonder if it does that for all tags, or are JS tags buggy? Try creating a <p> with no content and see if that self closes.

Note