Home > Web Front-end > JS Tutorial > jQuery dynamically loading js script file implementation method

jQuery dynamically loading js script file implementation method

php中世界最好的语言
Release: 2018-04-23 14:57:56
Original
1480 people have browsed it

This time I will bring you the implementation method of jQuery dynamically loading js script files. What are the precautions for jQuery to dynamically load js script files. The following is a practical case, let's take a look.

Dynamic loadingJavascript is a very powerful and useful technology. This topic has been discussed a lot on the Internet, and I often use RequireJS and Dojo to load js

in some personal projects. They are very powerful, but sometimes the gain outweighs the gain. . If you are using jQuery, there is a built-in method for loading a single js file. You can use this method when you need to delay loading some js plug-ins or other types of files. Here’s how to use it!

1. The jQuery getScript() method loads JavaScript

jQuery has a built-in method to load a single js file; when the loading is complete, you can call back in Perform subsequent operations in function . The most basic way to use jQuery.getScript is this:

jQuery.getScript("/path/to/myscript.js", function(data, status, jqxhr) {
 /*   做一些加载完成后需要执行的事情 */ 
});
Copy after login

This getScript method returns a jqxhr, you can use it like the following:

jQuery.getScript("/path/to/myscript.js")
 .done(function() {
  /* 耶,没有问题,这里可以干点什么 */
 })
 .fail(function() {
  /* 靠,马上执行挽救操作 */
});
Copy after login

The most common place to use jQuery.getScript It is to delay loading a js plug-in and execute it when the loading is completed:

jQuery.getScript("jquery.cookie.js")
 .done(function() {
  jQuery.cookie("cookie_name", "value", { expires: 7 });
});
Copy after login

2. Caching problem

There is a very important problem when using jQuery.getScript , you need to use a timestamp string after the js address that needs to be loaded to prevent it from being cached. However, if you want this script to be cached, you need to set the global cache variable, like this:

jQuery.ajaxSetup({
  cache: true
});
Copy after login
jQuery.ajax({
      url: "jquery.cookie.js",
      dataType: "script",
      cache: true
}).done(function() {
  jQuery.cookie("cookie_name", "value", { expires: 7 });
});
Copy after login


Be careful about caching issues when loading scripts!

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Jquery LigerUI detailed explanation of file upload steps

jquery dynamically loaded js file detailed explanation

The above is the detailed content of jQuery dynamically loading js script file implementation method. For more information, please follow other related articles on the PHP Chinese website!

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