Summary of dynamic loading of css using javascript

Source: Internet
Author: User

Customer home (www.bkjia.com)1. load required files in external CSS files

@ Importurl(style.css );

// It can only be used in CSS files or style labels

2. Simply load an external CSS file in the page

Document. createStyleSheet (cssFile );

2. Use the createElement method to create the CSS Link tag

Varhead = document. getElementsByTagName ('head'). item (0 );

Varstyle = document. createElement ('link ');

Style.hrefw.'style.css ';

Style. rel = 'stylesheet ';

Style. type = 'text/css ';

Head. appendChild (style );

The following are two frequently used functions.

  1. FunctionloadJs (file ){
  2. VarscriptTag = document. getElementById ('loadscript ');
  3. Varhead = document. getElementsByTagName ('head'). item (0 );
  4. If (scriptTag) head. removeChild (scriptTag );
  5. Script = document. createElement ('script ');
  6. Script. src = "../js/mi _" + file + ". js ";
  7. Script. type = 'text/javascript ';
  8. Script. id = 'loadscript ';
  9. Head. appendChild (script );
  10. }
  11. FunctionloadCss (file ){
  12. VarcssTag = document. getElementById ('loadcss ');
  13. Varhead = document. getElementsByTagName ('head'). item (0 );
  14. If (cssTag) head. removeChild (cssTag );
  15. Css = document. createElement ('link ');
  16. Css. href = "../css/mi _" + file + ". css ";
  17. Css. rel = 'stylesheet ';
  18. Css. type = 'text/css ';
  19. Css. id = 'loadcss ';
  20. Head. appendChild (css );
  21. }
Copy code

Related Article

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.