这次给大家带来jquery动态加载js文件实现步骤,jquery动态加载js文件的注意事项有哪些,下面就是实战案例,一起来看一下。
问题:
如果用jquery append直接加载script标签的话,会报错的。除了document.write外,还有没有其他的比较好的动态加载js文件的方法。
解决方法:
1、jquery方法
$.getscript(./test.js); //加载js文件
$.getscript(./test.js,function(){ //加载test.js,成功后,并执行回调函数
console.log(加载js文件);
});
2、js方法
<html>
<body>
</body>
</html>
<script type="text/javascript">
function loadscript(url, callback) {
var script = document.createelement(script);
script.type = text/javascript;
if(typeof(callback) != undefined){
if (script.readystate) {
script.onreadystatechange = function () {
if (script.readystate == loaded || script.readystate == complete) {
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = function () {
callback();
};
}
}
script.src = url;
document.body.appendchild(script);
}
loadscript(jquery-latest.js, function () { //加载,并执行回调函数
alert($(window).height());
});
//loadscript(jquery-latest.js); //加载js文件
</script>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
jquery插件uploadify使用详解
jquery让浏览器相互跳转传递参数使用详解
jquery基础知识点使用详解
以上就是jquery动态加载js文件实现步骤的详细内容。