一個簡單的動態載入js和css的jquery代碼_jquery

來源:互聯網
上載者:User

一個簡單的動態載入js和css的jquery代碼,用於在產生頁面時通過js函數載入一些共通的js和css檔案。

//how to use the function below: //$.include('file/ajaxa.js');$.include('file/ajaxa.css'); //or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa.css']);(only if .js and .css files are in the same directory) $.extend({ includePath: '', include: function(file) { var files = typeof file == "string" ? [file] : file; for (var i = 0; i < files.length; i++) { var name = files[i].replace(/^\s|\s$/g, ""); var att = name.split('.'); var ext = att[att.length - 1].toLowerCase(); var isCSS = ext == "css"; var tag = isCSS ? "link" : "script"; var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/javascript' "; var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'"; if ($(tag + "[" + link + "]").length == 0) $("head").prepend("<" + tag + attr + link + "></" + tag + ">"); } } }); $.include('../js/jquery-ui-1.8.21.custom.min.js'); $.include('../css/black-tie/jquery-ui-1.8.21.custom.css');

將該函數寫入一個common.js檔案中,在html中載入該common.js檔案,就可以達到目的。
注意:
1.在html5中,<script>標籤已經不支援language屬性了,所以我刪除了:

var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";

中的language='javascript'
2.原作者在寫入js和css標籤時,用的是:

document.write("<" + tag + attr + link + "></" + tag + ">");

但是經過實踐,發現document.write()方法會在寫入前清除原頁面的所有內容,也就相當於覆蓋的意思,這樣明顯達不到我的需要,我需要在載入頁面時動態向頁面匯入共通的js和css,而不能清除我原頁面的其他任何內容,所以查了下api,我改用了:

$("head").prepend("<" + tag + attr + link + "></" + tag + ">");

這個方法,$("head").prepend()方法的作用是在<head>標籤的最前端追加寫入內容。

最後,再補充一個方法,也是通過共通js來實現,應該比上面這個方法更容易理解:

Dynamically loading external JavaScript and CSS files To load a .js or .css file dynamically, in a nutshell, it means using DOM methods to first create a swanky new "SCRIPT" or "LINK" element, assign it the appropriate attributes, and finally, use element.appendChild() to add the element to the desired location within the document tree. It sounds a lot more fancy than it really is. Lets see how it all comes together: function loadjscssfile(filename, filetype){ if (filetype=="js"){ //if filename is a external JavaScript file var fileref=document.createElement('script') fileref.setAttribute("type","text/javascript") fileref.setAttribute("src", filename) } else if (filetype=="css"){ //if filename is an external CSS file var fileref=document.createElement("link") fileref.setAttribute("rel", "stylesheet") fileref.setAttribute("type", "text/css") fileref.setAttribute("href", filename) } if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref) } loadjscssfile("myscript.js", "js") //dynamically load and add this .js file loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.