1. 程式人生 > >js 動態新增 外部js css 到head標籤

js 動態新增 外部js css 到head標籤

 function appendJQCDN() {
     var head = document.head || document.getElementsByTagName('head')[0];
     var script = document.createElement('script');
     var style = document.createElement('style');
     script.setAttribute("src", "https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js");
     style.innerHTML = '';
     head.appendChild(script);
     head.appendChild(style);
	
 }
動態新增 js
 function loadScript(url, callback) {
     var script = document.createElement("script");
     script.type = "text/javascript";
     if (typeof (callback) != "undefined") {
         if (script.readyState) {
             script.onreadystatechange = function () {
                 if (script.readyState == "loaded" || script.readyState == "complete") {
                     script.onreadystatechange = null;
                     callback();
                 }
             };
         } else {
             script.onload = function () {
                 callback();
             };
         }
     };
     script.src = url;
     document.body.appendChild(script);
 }
loadScript("https://cdn.bootcss.com/jquery/3.2.1/jquery.js", function() { 

      //載入,並執行回撥函式

      alert('動態引入jquery成功')

});