当网站需求变大,脚本的需求也逐步变大。我们就不得不引入太多的JS 脚本而降低了
整站的性能,所以就出现了动态脚本的概念,在适时的时候加载相应的脚本。
比如:我们想在需要检测浏览器的时候,再引入检测文件。
var flag = true; //设置true 再加载
if (flag) {
  loadScript('browserdetect.js'); //设置加载的js
}
function loadScript(url) {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = url;
  //document.head.appendChild(script); //document.head 表示<head>
  document.getElementsByTagName('head')[0].appendChild(script);
}
PS:document.head 调用,IE 不支持,会报错!

相关文章:

  • 2021-06-07
  • 2021-09-14
  • 2022-02-04
  • 2022-02-11
  • 2022-12-23
  • 2021-10-17
  • 2021-07-07
  • 2022-12-23
猜你喜欢
  • 2021-12-05
  • 2021-08-08
  • 2021-10-20
  • 2022-02-02
  • 2021-05-29
相关资源
相似解决方案