JS Dynamic Introduction js,css--Dynamically create Script/link/style tags

Source: Internet
Author: User

I. Dynamic creation of link methods

We can use link in the same way as shown in the following code.

[JavaScript]View Plaincopyprint?
  1. function Addcssbylink (URL) {
  2. var doc=document;
  3. var link=doc.createelement ("link");
  4. Link.setattribute ("rel", "stylesheet");
  5. Link.setattribute ("type", "text/css");
  6. Link.setattribute ("href", url);
  7. var heads = doc.getelementsbytagname ("Head");
  8. if (heads.length)
  9. Heads[0].appendchild (link);
  10. Else
  11. Doc.documentElement.appendChild (link);
  12. }


    • var doc=document;
  • var style=doc.createelement ("style");
  • Style.setattribute ("type", "text/css");
  • if (style.stylesheet) {//IE
  • Style.styleSheet.cssText = cssstring;
  • } else {//
  • var csstext = Doc.createtextnode (cssstring);
  • Style.appendchild (Csstext);
  • }
  • var heads = doc.getelementsbytagname ("Head");
  • if (heads.length)
  • Heads[0].appendchild (style);
  • Else
  • Doc.documentElement.appendChild (style);
  • }

In this case, if it is less code, it can be more convenient to implement the effect of dynamic loading CSS, but if in order to facilitate maintenance and management, and do not wait for the time limit, the use of link method more appropriate

"Script");

    • Script.setattribute ("type", "Text/javascript");
    • Script.setattribute ("src", "justwalking.js");
    • var heads = document.getElementsByTagName ("Head");
    • if (heads.length)
    • Heads[0].appendchild (script);
    • Else
    • Document.documentElement.appendChild (script);

But this way in the IE kernel browser support, Google, 360 speed, Firefox but not

Four. Printing introduces style mode

[JavaScript]View Plaincopyprint?
    1. document.write ("<link rel=\" stylesheet\ "href=\" uild/style.css\ "type=\" text/css\ "media=\" screen\ "/>");
Five. Printing introduces JS mode [JavaScript]View Plaincopyprint?
    1. document.write ("<script type=\" text/javascript\ "src=\" justwalking.js\ "></script>");


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.