IE8 a solution for dynamically loading CSS using JavaScript _javascript tips

Source: Internet
Author: User
Tags try catch

It is well known to do front-end development are eager to kick ie developers a few feet, ie developer reputation of the difference is not less than GFW developers, ignorant of the conscience of the market, everyone has to punish, but in China where the market share, there is no way can only bow to the reality.

Recently our products need to dynamically load a CSS in the browser, the previous code is directly used:

Copy Code code as follows:

var bubblecss = document.createelement (' style ');
Bubblecss.type = ' text/css ';
bubblecss.innerhtml = Blc_conf.bubblestyle;
document.getElementsByTagName (' head ') [0].appendchild (BUBBLECSS);

But this only IE9 support, under the IE8 will be a problem, has not noticed this piece, until the recent refactoring to do a complete test when the discovery.
Search on the Internet a skill, tried, workable, but there are some problems
Copy Code code as follows:

Window.bc_bubble_css = Blc_conf.bubblestyle;
Document.createstylesheet ("Javascript:bc_bubble_css");

Here you can create a style defined by the variable bc_bubble_css, but because HTML5 is becoming popular, we also have some CSS3 selector in our CSS, and using this method will cause IE8 parser to parse to CSS3 Selector the time to throw the exception and stop parsing subsequent CSS, which makes the CSS only half loaded, online search methods are used stylesheet type of addrule to increase, but this need to specify their own CSS2 selector and style,
So you need to take a single rule from the CSS and then call addrule in turn, example:
Copy Code code as follows:

var s = document.createstylesheet ();
var rules = Blc_conf.bubbleStyle.replace (/\/\*[^\*]*\*\//g, ""). Replace (/@[^{]*\{/g, '). Match (/[^\{\}]+\{[^\}]+\}/ g);
for (var i = 0; i < rules.length; i++) {
var m = Rules[i].match (/(. *) \s*\{\s* (. *) \}/);
if (m) {
try {
S.addrule (M[1], m[2]);
catch (e) {
}
}
}

There are two replacements at the beginning, which remove the gaze and part of the CSS3 selector, but there are still selector that need to be caught in the back try catch.

Again despise the person who designs IE interface

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.