Dynamically Adding Script Tags with Uncontrolled Source Content
Creating a script tag with a source external to your control can pose a challenge when the content includes code that utilizes document.write(). As noted, simply appending the script tag in the
cannot support such content.To overcome this, consider the following solution:
var my_awesome_script = document.createElement('script'); my_awesome_script.setAttribute('src','http://example.com/site.js');
document.head.appendChild(my_awesome_script);
This method effectively includes the external script dynamically, allowing the included code to execute without any issues.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3