Home > Web Front-end > JS Tutorial > body text

Use jquery to dynamically load javascript to reduce server pressure_jquery

WBOY
Release: 2016-05-16 17:48:41
Original
1043 people have browsed it

Fortunately, jQuery provides a built-in utility function, $getScript() provides us with such a method. To implement dynamic functions on web pages. Let’s take a look at the syntax of this function:

$.getScript(url,callback)

Get the script specified by the url parameter and use a GET request to the specified server

Parameters
URL() gets the URL of the script file.

callback (Function) optional function calls the function inside the script file after loading

The specific implementation process is

loading text from resources

|If character If the string is successful,

returns the XHR instance, which is used to obtain the script.

How to use it? See the method below

We create a new.js script file

Copy code The code is as follows:

var testVar = 'New JS loaded!';
alert(testVar);
function newFun(dynParam)
{
alert( 'You just passed ' dynParam ' as parameter.');
}

HTML code
Copy code The code is as follows:



$.getScript Example








In the above code, we create a new.js script file and call it in the body part when the button function is triggered. The advantage of doing this is to reduce the pressure on the server, which is highly recommended.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!