开发者社区> 问答> 正文

用原生js怎么动态添加一个js文件?

代码:

var scriptFile = document.createElement('script');

scriptFile.setAttribute("type","text/javascript");

scriptFile.setAttribute("src",'http://api.map.baidu.com/api?type=quick&ak=o9B4Ol99j9NcBXSu5nFTR7uI&v=1.0');

document.getElementsByTagName("head")[0].appendChild(scriptFile);
最后要添加到head里的时候,报这个错:Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

我其它普通的js是可以的,但是百度的这个似乎不行,请问解决方法,或者其它添加js的方法(使用ajax的方法也行,只要是原生js就好了)

展开
收起
a123456678 2016-07-29 12:00:13 2775 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
JavaScript函数 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载