/    Sign up×
Community /Pin to ProfileBookmark

Changing CSS styles via Stylesheet VS DOM

I’m making a webpage which used to have % widths for my divs, but these would display too erratically in different browsers, so I had to switch to static widths. I got the idea though to recalculate the widths based on the pane width on resize and this works nice. I’m debating though, whether it is better to change the styles for my properties via the stylesheet collection, or via the DOM. Here are the two scripts:

Stylesheet Collection

[code=php]function switchTableSize(width, height)
{
if (typeof document.styleSheets != “undefined”) {
var styleSheet = document.styleSheets[1];
var rules = null;
var chemWidth = Math.floor((width-72)/18);

if (typeof styleSheet.rules != “undefined”)
rules = styleSheet.rules;
else
rules = styleSheet.cssRules;

for (var i = 0; i < rules.length; i++) {
switch (rules[i].selectorText)
{
case “#container”:
rules[i].style.width = width + “px”;
break;

case “.element”:
rules[i].style.width = chemWidth + “px”;
break;

case “.spacer”:
rules[i].style.width = chemWidth*3 + 12 + “px”;
break;

default:
break;
}
}
}
}[/code]

DOM

[code=php]function switchTableSize(width, height)
{
var chemWidth = Math.floor((width-72)/18);
document.getElementById(‘container’).style.width = width + ‘px’;

var divs = document.getElementsByTagName(‘div’);
for (var i = 0; i < divs.length; i++) {
var div = divs[i]
if (div.className && (‘ ‘ + div.className + ‘ ‘).indexOf(‘ element ‘) != -1) {
div.style.width = chemWidth + ‘px’;
} else if (div.className && (‘ ‘ + div.className + ‘ ‘).indexOf(‘ spacer ‘) != -1) {
div.style.width = chemWidth*3 + 12 + ‘px’;
}
}
}[/code]

On one hand, the DOM version is shorter (in code) and works with more browsers. However, there are about 175 divs in my page, 150 of which fall under the ‘element’ className, and 2 which fall under the ‘spacer’ className, (not to mention I’m using an indexOf call) so it seems like it might be resource intensive.

On the other hand, the stylesheet collection version seems more elegant, only changes 3 properties (and the CSS page in question has 10 properties total). However there is slightly less browser support for this method.

Any thoughts on this?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@mrhooNov 24.2006 — You could detect if the client supports document.styleSheets (IE,Firefox,Seamonkey)and write the rule to those that do.

For the others,append a new style element

to the head of the document with the cssText as a textNode.
Copy linkTweet thisAlerts:
@GotMex_authorNov 24.2006 — Ok yea, scratch that question... it takes like 4 seconds to resize in Netscape 6... it's too intensive for poor netscape. I'm gonna try mrhoo's idea of adding a style element to the head instead.
Copy linkTweet thisAlerts:
@mrhooNov 24.2006 — The createElement('style') works in most of the browsers,

but for IE only you have to add the text via

the element.styleSheet.cssString property- the others let

you append a new textNode to the element. Something like this:

[CODE]function(str,t){
var pa= document.getElementsByTagName('head')[0];
var el= document.createElement('style');
el.setAttribute('type','text/css');
el.setAttribute('media','screen');
if(t)el.setAttribute('title',t);

if(el.styleSheet) el.styleSheet.cssText= str;
//IE alone has (or needs) element.styleSheet

else el.appendChild(document.createTextNode(str));
pa.appendChild(el);
return el;
}[/CODE]
×

Success!

Help @GotMex_ 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 5.16,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...