Whenever we want to load the script file we would have to create an element of type “script” using document.createElement. We would then specify its src attribute to be the location of the script either CDN or a local js file. Once that’s done, we will have to append the tag to an HTML element that’s already existing in the DOM. For example, the head element.
Example 1: In this example, we are loading a
load.js file from file system dynamically on component load.
Output: As soon as the app.component loads, the script loads as well and displays the alert window.
Example 2: In this example, we will load the
load.js script on button click.
Output: As the button is pressed, the JS file loads.
- How to dynamically load JS inside JS ?
- How to dynamically get the content width of a div using AngularJS ?
- How to dynamically get the content height of a div using AngularJS ?
- How to add input fields dynamically on button click in AngularJS ?
- How to execute AngularJS controller function on page load ?
- How to debug PHP scripts ?
- Introduction to NPM scripts
- HTML | DOM scripts Collection
- Exporting Data from scripts in R Programming
- How to set alternate content for users that do not support client-side scripts ?
- How to use External Modules and NPM in a project ?
- How to define the relationship between a document and the external resources?
- How to generate an XML file dynamically using PHP?
- Dynamically generating a QR code using PHP
- How to change favicon dynamically?
- How to create dictionary and add key–value pairs dynamically?
- How to Dynamically Add/Remove Table Rows using jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.