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

Detailed explanation of jquery dynamically loading js files

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

This time I will bring you jquery a detailed explanation of dynamically loading js files. What are the precautions for jquery to dynamically load js files? The following is a practical case, let’s take a look.

$.extend({
     
include
Path: '',
     include: function(file) {
        var files = typeof file == "string" ? [file]:file;
        for (var i = 0; i < files.length; i++) {
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split(&#39;.&#39;);
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type=&#39;text/css&#39; rel=&#39;stylesheet&#39; " : " language=&#39;
javascript
&#39; type=&#39;text/javascript&#39; ";
            var link = (isCSS ? "href" : "src") + "=&#39;" + $.includePath + name + "&#39;";
            if ($(tag + "[" + link + "]").length == 0) $("body").append("<" + tag + attr + link + "></" + tag + ">");
        }
   }
});
//使用方法
$.includePath = &#39;../js/Exam&#39;; 
$.include([&#39;/AddQuestion.js&#39;]);
Copy after login

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:

How jQuery reads the content of XML files

JQuery AJAX implementation file download

The above is the detailed content of Detailed explanation of jquery dynamically loading js files. For more information, please follow other related articles on the PHP Chinese website!

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