This article is all about fetching a JS file from any jQuery (CDN) link or React file in Document Object Model.
- Create a new script element.
let newscript = document.createElement('script');
- Provide the source to the newly created script element.
newscript.src = document.getElementById("source").value;
- Append the element in the DOM head so that it could execute the script.
The above approach is implemented below in the example by considering the following file resources as for example scripts that are loaded dynamically.
Resources: You can use this CDn link in the input field to get fetched.
React CDN :
Note: We will be using these files. (User can use any source as per their requirement).
- jQuery CDN:
- Dynamically loading ReactJS :
- loading Custom JS :
- User can use any events on the element to load the script. In this article.
“onclick” event has been used.
- Scripts should not contain errors otherwise they may throw errors and may cause the immediate stoppage of code execution.
- Use proper error handling so that the execution of code doesn’t get interrupted if any error occurred while fetching.
- Above implementation is also valid for ElectronJS.