How to use jQuery getScript ?
Last Updated :
23 Apr, 2021
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 =
</ script >
</ head >
< body style = "text-align: center" >
< h2 id = "heading" >GeeksforGeeks</ 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(
function () {
// Selecting button and adding event to it.
$("#button").click(function () {
$("#heading")
.animate({ backgroundColor: "rgb(0, 255, 0)" },
500);
});
});
</ script >
</ body >
</ html >
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...