动态加载js的几种方法

  1.直接document.write

  <script language=javascript>

  document.write(<script src='test.js'><\/script>);

  </script>

  2.动态改变已有script的src属性

  <script src='' id=s1></script>

  <script language=javascript>

  s1.src=test.js

  </script>

  3.动态创建script元素

  <script>

  var s2 = document.createElement(script);

  s2.src=test.js

  document.body.insertAdjacentElement(BeforeBegin,s2);

  </script>

  <head>

  <script>

  function loadJs(file){

  var scriptTag = document.getElementById('loadScript');

  var head = document.getElementsByTagName('head').item(0)

  if(scriptTag) head.removeChild(scriptTag);

  script = document.createElement('script');

  script.src = file;

  script.type = 'text/javascript';

  script.id = 'loadScript';

  head.appendChild(script)

  }

  </script>

  </head>

  <body onload=loadJs('1.js');loadJs('2.js')>