Decoded Personalized Google code

A

Aaron Gray

Hi,

Well I have gone and done it, got to the bottom of it.

Nothing spectacular but its good to see how it is done.

Now to distill the brew ...

I am going to use sub threads of the thread to air the reworked Google code
for comments, suggestions and ammendments.

Hope you will join the fun, and I hope Google do not mind us having the fun.

My reason in using rewritten Google code is it should result in very
portable code as a result.

Aaron
 
A

Aaron Gray

Aaron Gray said:
Well I have gone and done it, got to the bottom of it.

Nothing spectacular but its good to see how it is done.

Now to distill the brew ...

First up 'SetClassStyle()' :-

//
// SetClassStyle() - Sets a classes style to a value
//
// Works pre stylesheet by setting the style of all elements of the
particular class.
//

function SetClassStyle( clazz, style, value)
{
if (document.styleSheets)
{
clazz = "." + clazz;

for( var styleSheetNo = 0; styleSheetNo < document.styleSheets.length;
styleSheetNo++)
{
var styleSheet = document.styleSheets[styleSheetNo];
var rules = styleSheet.rules | styleSheet.cssRules;

if (!rules) return; // err ???

for ( var rule = 0; rule < rules.length; rule++)
if ( rules[rule].selectorText.toLowerCase() == clazz)
rules[rule].style[style] = value;
}
}
else // No stylesheet
{
var all = getElementByTagName("*");

for ( var element = 0; element < all.length; element++)
if ( all[element].className == clazz)
all[element].style[style] = value;
}
}

Anyone enlighten me on the cop out return statement, 'err ???'.

Should it stay or should it go. Or what ?

Aaron
 
A

Aaron Gray

Aaron Gray said:
Aaron Gray said:
Well I have gone and done it, got to the bottom of it.

Nothing spectacular but its good to see how it is done.

Now to distill the brew ...

First up 'SetClassStyle()' :-

//
// SetClassStyle() - Sets a classes style to a value
//
// Works pre stylesheet by setting the style of all elements of the
particular class.
//

function SetClassStyle( clazz, style, value)
{
if (document.styleSheets)
{
clazz = "." + clazz;

for( var styleSheetNo = 0; styleSheetNo < document.styleSheets.length;
styleSheetNo++)
{
var styleSheet = document.styleSheets[styleSheetNo];
var rules = styleSheet.rules | styleSheet.cssRules;

if (!rules) return; // err ???

for ( var rule = 0; rule < rules.length; rule++)
if ( rules[rule].selectorText.toLowerCase() == clazz)
rules[rule].style[style] = value;
}
}
else // No stylesheet
{
var all = getElementByTagName("*");

for ( var element = 0; element < all.length; element++)
if ( all[element].className == clazz)
all[element].style[style] = value;
}
}

Anyone enlighten me on the cop out return statement, 'err ???'.

Should it stay or should it go. Or what ?

Aaron
 
A

Aaron Gray

var rules = styleSheet.rules | styleSheet.cssRules;

Is this form totally portable or should it be done with 'if' statements ?

Aaron
 
M

Matt Kruse

Aaron said:
First up 'SetClassStyle()' :-

This doesn't seem to be very generalized. What are you hoping to gain by
posting it?
clazz = "." + clazz;

Only sets the style for rules of the form

..class

but not for

tagname.class
or
tag .class
if (!rules) return; // err ???

Should it not then default to the "No stylesheet" approach?
var all = getElementByTagName("*");

Error prone in some versions of IE, no?
if ( all[element].className == clazz)

Not a good general practice, since an element may have style="class1 class2"
and this case would be skipped over.
 
A

Aaron Gray

Matt Kruse said:
This doesn't seem to be very generalized. What are you hoping to gain by
posting it?

Ah. Its not really a general routine then :(

The leading period can go.
Only sets the style for rules of the form

.class

but not for

tagname.class
or
tag .class


Should it not then default to the "No stylesheet" approach?

Right, that is probably or possibility the right solution.
var all = getElementByTagName("*");

Error prone in some versions of IE, no?
if ( all[element].className == clazz)

Not a good general practice, since an element may have style="class1
class2" and this case would be skipped over.

Ah. I was not aware of that. But again this is not really a general routine
but one to be used as part of a UI library.
Which is a limited case. But I will bear that in mind if I can get my head
round it ??

Trying to produce a proper general case would be good but it may be too much
work or be inefficient for the cases it is being used in.

Aaron
 
A

Aaron Gray

Aaron Gray said:
Is this form totally portable or should it be done with 'if' statements ?

| does not work for undefines, nulls only :(

Where as 'if' statement gives false on an undefined variable.

Should have checked that properly !

Aaron
 
A

Aaron Gray

Aaron Gray said:
Hi,

Well I have gone and done it, got to the bottom of it.

Nothing spectacular but its good to see how it is done.

Now to distill the brew ...

function getElementById( a)
{
return document.getElementById ? document.getElementById(a) : null
}

Can the null be replaced by :-

document[ a]

Or anthing else ?

Aaron
 
A

Aaron Gray

Or even a question mark colon :)

var rules = styleSheet.rules ? styleSheet.rules : styleSheet.cssRules;

Thats better,

Aaron
 
A

Aaron Gray

Aaron Gray said:
Hi,

Well I have gone and done it, got to the bottom of it.

Nothing spectacular but its good to see how it is done.

Now to distill the brew ...

function getElementByTagName( a)
{
return document.getElementsByTagName ? document.getElementsByTagName(a) :
new Array()
}

And this one ... anything that can be done here ?

Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top