Question:
How can I dynamically add a script tag with an unrestrained src that may contain document.write calls?
Explanation:
In scripts with uncontrolled src attributes, the content may include document.write calls, which can interfere with traditional script tag loading.
Solution:
To circumvent this issue, create a new script element manually:
var my_awesome_script = document.createElement('script'); my_awesome_script.setAttribute('src','http://example.com/site.js');
Append the newly created script element to the head of the document:
document.head.appendChild(my_awesome_script);
This method ensures that the script is loaded asynchronously without interfering with the document flow.
The above is the detailed content of How to Safely Include Dynamic Scripts with Uncertain Src Content?. For more information, please follow other related articles on the PHP Chinese website!