How to load up CSS files using Javascript?

后端 未结 19 3330
栀梦
栀梦 2020-11-21 07:51

Is it possible to import css stylesheets into a html page using Javascript? If so, how can it be done?

P.S the javascript will be hosted on my site, but I want users

19条回答
  •  梦谈多话
    2020-11-21 08:05

    I guess something like this script would do:

    
    

    This JS file contains the following statement:

    if (!document.getElementById) document.write('');
    

    The address of the javascript and css would need to be absolute if they are to refer to your site.

    Many CSS import techniques are discussed in this "Say no to CSS hacks with branching techniques" article.

    But the "Using JavaScript to dynamically add Portlet CSS stylesheets" article mentions also the CreateStyleSheet possibility (proprietary method for IE):

    
    
                                     
                  
提交回复
热议问题