不使用script导入js文件的几种方法

前端之家收集整理的这篇文章主要介绍了不使用script导入js文件的几种方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方法一:原生

 adc.js内容如下:

502_6@

html.html

var s = document.createElement("script"); s.src = "abc.js"; document.head.appendChild(s); s.addEventListener("load",function(){ // 等待s的load事件加载完响应,防止未加载完就调用出错 console.log(hello); })
  setTimeout(function(){//或者使用定时器保证其载入完后<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>(不安全,不如监听事件好)
    console.log(hello);
  },1000);
 // $.getScript("abc.js");
@H_502_6@

方法二:jquery.js

502_6@

方法三:require.js

require.js分享2.1.1版本,注意是针对大项目使用,一边情况下使用jquery即可。

index.html

猜你在找的JavaScript相关文章