Home  >  Article  >  Web Front-end  >  jquery plugin_html/css_WEB-ITnose that dynamically loads js and css

jquery plugin_html/css_WEB-ITnose that dynamically loads js and css

WBOY
WBOYOriginal
2016-06-24 11:58:52965browse

A simple jquery code for dynamically loading js and css, which is used to load some common js and css files through js functions when generating pages.

Java code

  1. //how to use the function below:
  2. //$.include('file/ ajaxa.js');$.include('file/ajaxa.css');
  3. //or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa .css']);(only if .js and .css files are in the same directory)
  4. $.extend({
  5. includePath: '',
  6. include : function(file)
  7. {
  8. var files = typeof file == "string" ? [file] : file;
  9. for (var i = 0; i < ; files.length; i )
  10. {
  11. var name = files[i].replace(/^s|s$/g, "");
  12. var att = name.split('.');
  13. var ext = att[att.length - 1].toLowerCase();
  14. var isCSS = ext == "css"; 🎜>
  15. var tag = isCSS ? "link" : "script";
  16. var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/ javascript '";
  17. var link = (iSCSS?" HREF ":" SRC ")" $ .includepath name "'";
  18. if (tag "[" ["[" link "]").length == 0) $("head").prepend("<" tag attr link ">");
  19. }
  20. }
  21. });
  22. $.include('../js/jquery-ui-1.8.21.custom.min.js');
  23. $.include('../css/black-tie/jquery-ui-1.8.21.custom.css');
Write this function into a common. js file, loading the common.js file in html can achieve the purpose. This js function comes from the following link:
http://www.cnblogs.com/chenjinfa/archive/2009/03/17/1414178.html
Note:
1. In html5,

Java code

    var attr = isCSS ? " type='text /css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
language='javascript' in
2. Original When the author writes js and css tags, he uses:

Java code

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

But after practice, I found that the document.write() method will clear all the contents of the original page before writing, which is equivalent to overwriting. This obviously does not meet my needs. I need to dynamically load the page. Importing common js and css into the page, but not clearing any other content of my original page, so I checked the api and I used:

Java code

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

The function of this method, the $("head").prepend() method is to append written content to the front end of the tag.

Finally, add another method, which is also implemented through common js. It should be easier to understand than the above method:

Java code

  1. Dynamically loading external JavaScript and CSS files
  2. 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:
  3. function loadjscssfile(filename, filetype){
  4. if (filetype=="js"){ //if filename is a external JavaScript file
  5. var fileref=document.createElement('script')
  6. fileref.setAttribute("type","text/javascript")
  7. fileref.setAttribute("src ", filename)
  8. }
  9. else if (filetype=="css"){ //if filename is an external CSS file
  10. var fileref=document.createElement( "link")
  11. fileref.setAttribute("rel", "stylesheet")
  12. fileref.setAttribute("type", "text/css")
  13. fileref. setAttribute("href", filename)
  14. }
  15. if (typeof fileref!="undefined")
  16. document.getElementsByTagName("head")[0].appendChild (fileref)
  17. }
  18. loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
  19. loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
  20. loadjscssfile("mystyle.css", "css") //dynamically load and add this .css file

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn