js异步加载的方式有哪些?

JavaScript012

js异步加载的方式有哪些?,第1张

方法一:Script Dom Element

(function(){

var scriptEle = document.createElement("script")

scriptEle.type = "text/javasctipt"

scriptEle.async = true

scriptEle.src = "http://cdn.bootcss.com/jquery/3.0.0-beta1/jquery.min.js"

var x = document.getElementsByTagName("head")[0]

x.insertBefore(scriptEle, x.firstChild)

})()

方法二:onload时的异步加载

function(){

if(window.attachEvent){

window.attachEvent("load", asyncLoad)

}else{

window.addEventListener("load", asyncLoad)

}

var asyncLoad = function(){

var ga = document.createElement('script')

ga.type = 'text/javascript'

ga.async = true

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'

var s = document.getElementsByTagName('script')[0]

s.parentNode.insertBefore(ga, s)

}

})()

方法三:$(document).ready()

你好,这个问题其实很简单,如果异步加载的JS可以直接修改,则可以通过在该JS文件中手动添加“debugger”语句实现自动断点。例如异步加载的JS文件内容为:

(function(window){

var num = Math.random() * 1000

debugger// 调完记得删除

num += 100

console.log(num)

})(window)

待调试完毕后,删除该语句即可。

但是,如果被异步加载的JS不方便随意修改,则可以将JS存到本地,使用Fiddler等代理软件,将JS代理到本地,再使用以上方法进行调试。待修复完毕后删除调试代码,上传覆盖远程JS文件即可。

另外,我曾经写过一篇文章《一探前端开发中的JS调试技巧》有更详细的演示,只需前往百度首页搜索文章标题,即可得到文章地址。

希望是你想要的答案,如有疑问可追问,望采纳~~