Home  >  Article  >  Web Front-end  >  Three ways to dynamically load JS files_javascript skills

Three ways to dynamically load JS files_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:16:531050browse

Just look at the example.
Example 1 Reload js file

Copy code The code is as follows:

function loadJs(file) {
      var head = $("head").remove("script[role='reload']");
                                                           "").attr({ role: 'reload', src: file, type: 'text/javascript' }).appendTo(head);
}

Example 2 How to reload the javascript file (give js an id), encapsulate it into a method for everyone to use:
Copy the code The code is as follows:

function reloadAbleJSFn(id,newJS)
{
var oldjs = null;
var t = null;
var oldjs = document.getElementById(id);
if(oldjs) oldjs.parentNode.removeChild(oldjs);
var scriptObj = document.createElement("script");
scriptObj.src = newJS;
scriptObj.type = "text/javascript";
scriptObj.id = id;
document.getElementsByTagName("head")[0].appendChild(scriptObj);
}

Example 3 For jquery, just use getScript directly.
Copy code The code is as follows:




< ;body>

Statement:
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