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

动态加载JS文件的三种方法_javascript技巧

WBOY
Release: 2016-05-16 17:16:53
Original
1016 people have browsed it

直接看实例。
例1 重新加载js文件

复制代码 代码如下:

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

例2 重新载入javascript文件的方法(给js定个id),自己封装成一个方法方便大家使用:
复制代码 代码如下:

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);
}
 

例3 jquery的就直接使用getScript就可以了。
复制代码 代码如下:






 
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!