/    Sign up×
Community /Pin to ProfileBookmark

combining MSIE and Standards version of script

How to combine these:
‘addRule()’ for MSIE and ‘insertRule()’ for standards-compliant browsers: so far, when I test on Firefox or IE8, one of the browsers always chokes on the version of the script which doesn’t apply to it.

I’ve also tried using conditional comments to separate out the msie version, but that doesn’t seem to work either, as the scripts still have to be called, and thatr’s where the choke happens.

Here’s the full (clunky) versions; they add a red fullstop:

var elA = document.getElementsByTagName(“*”);
var S = document.styleSheets[0];

//msie:
function IEaddDot1a() {
for (i=0; i<elA.length; i++) {
if (elA.item(i).className.charAt(0) == “A”) {
S.addRule(‘.A1:after’, ‘position:relative; top:0.45em; margin:0 0 0 -0.4em; font-size:80%; color:#f11; content: ” .”‘) }
}
}

//standards:
function addDot1a() {
for (i=0; i<elA.length; i++) {
if (elA.item(i).className.charAt(0) == “A”) {
S.insertRule(‘.A1:after {position:relative; top:0.45em; margin:0 0 0 -0.4em; font-size:80%; color:#f11; content:” .”}’,S.cssRules.length) }
}
}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@toicontienSep 14.2009 — Sounds like a great time to use a try-catch block:
[CODE]var style = {};

/**
* Insert a rule into a style sheet. Cross browser-safe
* @param object css Reference to a style sheet object
* @param string rule The CSS rule to insert
* @return void
*/
style.insertRule = function( css, rule ) {
try {
this.insertRuleStandard( css, rule );
this.insertRule = this.insertRuleStandard;
}
catch ( err ) {
this.insertRuleMSIE( css, rule );
this.insertRule = this.insertRuleMSIE;
}
};

style.insertRuleStandard = function( css, rule ) {
css.insertRule( rule );
};

style.insertRuleMSIE = function( css, rule ) {
css.addRule( rule );
};[/CODE]


The try-catch block in the style.insertRule function executes the first time you call this function. It dynamically reasigns itself to the function the user's browser supports. It defaults to the standard insertRule method.
Copy linkTweet thisAlerts:
@ctozauthorSep 14.2009 — Many thanks.


Slowly rebuilding a script-primitive page, starting at PPK's site. Bound to be back for more ?.
×

Success!

Help @ctoz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...