Dynamically loading JS and CSS for jquery plugin

Source: Internet
Author: User

A simple jquery code that dynamically loads JS and CSS to load common JS and CSS files using the JS function when generating a page.

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 is 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 + "  ; </"+ tag + " > ");
  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 the function into a common.js file and load the Common.js file in HTML to achieve the purpose. The JS function is derived from the following link:
Http://www.cnblogs.com/chenjinfa/archive/2009/03/17/1414178.html
Attention:
1. The,<script> tag in HTML5 does not already support the language attribute, so I deleted it:

Java code
    1. var attr = iscss?  "type= ' text/css ' rel= ' stylesheet '": "language= ' javascript ' type= ' Text/javascript '";


Language= ' JavaScript ' in the
2. When writing JS and CSS tags, the original author uses:

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


But through practice, found that the document.write () method will erase all the contents of the original page before writing, it is equivalent to the meaning of the overwrite, this obviously does not reach my needs, I need to load the page dynamically to the page to import common JS and CSS, and not to clear my original page of any other content, so check the next API, I use:

Java code
    1. $ ("Head"). Prepend ("<" + tag + attr + link + "></" + tag + ">");


This method, $ ("head"). The function of the prepend () method is to append the content to the front of the
Finally, to add a method, but also by common JS to achieve, should be more easily understood 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 the DOM methods to first create a swanky new "SC Ript "or " LINK "element, assign it the appropriate attributes, and Finally, use Element.appendchild () to add the E Lement 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

Dynamically loading JS and CSS for jquery plugin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.