How can a script tag with a variable src attribute be dynamically added to a webpage, especially if the src contains document.write functions?
Ordinarily, adding a script tag with a specific src attribute in the HTML head works seamlessly. However, when the src attribute includes document.write code, it becomes problematic.
To dynamically add such a script tag, the following steps can be taken:
var my_awesome_script = document.createElement('script');
my_awesome_script.setAttribute('src', 'http://example.com/site.js');
document.head.appendChild(my_awesome_script);
This script element will be dynamically added to the webpage, even if its src contains document.write code.
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