How to use jQuery getScript ?
The jQuery .getScript() method loads a JavaScript file from a server using the GET HTTP method and then executes it.
Syntax:
$.getScript(url,[callback])
Let us look at an example to understand how this method works.
Example: In this example, we will use a URL to load the JavaScript file ‘color.js’ from the server. The file’s functionality is used to add animation to our webpage.
HTML
<!DOCTYPE html> < html > < head > < script src = "https://code.jquery.com/jquery-3.5.0.js" > </ script > </ head > < body style = "text-align: center" > < h2 id = "heading" >w3wiki</ h2 > < p >jQuery | getScript() method</ p > < button id = "button" >click here</ button > < script > /* The getScript method is called by passing url to load color.js and calling a function to add color animations on our page */ $.getScript( "https://code.jquery.com/color/jquery.color.js", function () { // Selecting button and adding event to it. $("#button").click(function () { $("#heading") .animate({ backgroundColor: "rgb(0, 255, 0)" }, 500); }); }); </ script > </ body > </ html > |
Output: