"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Dynamically Include Scripts with Uncontrolled `document.write()` Content?

How to Dynamically Include Scripts with Uncontrolled `document.write()` Content?

Published on 2024-11-08
Browse:783

How to Dynamically Include Scripts with Uncontrolled `document.write()` Content?

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:

  1. Create a new script element using document.createElement('script').
  2. Assign the external source to the script's src attribute:
var my_awesome_script = document.createElement('script');

my_awesome_script.setAttribute('src','http://example.com/site.js');
  1. Append the script element to the of the document:
document.head.appendChild(my_awesome_script);

This method effectively includes the external script dynamically, allowing the included code to execute without any issues.

Latest tutorial More>

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