By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,639 Members | 2,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,639 IT Pros & Developers. It's quick & easy.

Decoded Personalized Google code

P: n/a
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
Nov 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Aaron Gray" <an********@gmail.com> wrote in message
news:3u************@individual.net...
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

Nov 23 '05 #2

P: n/a

"Aaron Gray" <an********@gmail.com> wrote in message
news:3u************@individual.net...
"Aaron Gray" <an********@gmail.com> wrote in message
news:3u************@individual.net...
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

Nov 23 '05 #3

P: n/a
> var rules = styleSheet.rules | styleSheet.cssRules;

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

Aaron
Nov 23 '05 #4

P: n/a
Aaron Gray wrote:
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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Nov 23 '05 #5

P: n/a
"Matt Kruse" <ne********@mattkruse.com> wrote in message
news:dl*********@news2.newsguy.com...
Aaron Gray wrote:
First up 'SetClassStyle()' :-
This doesn't seem to be very generalized. What are you hoping to gain by
posting it?
clazz = "." + clazz;
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
if (!rules) return; // err ???


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
Nov 23 '05 #6

P: n/a
"Aaron Gray" <an********@gmail.com> wrote in message
news:3u************@individual.net...
var rules = styleSheet.rules | styleSheet.cssRules;


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
Nov 23 '05 #7

P: n/a
"Aaron Gray" <an********@gmail.com> wrote in message
news:3u************@individual.net...
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
Nov 23 '05 #8

P: n/a
"Aaron Gray" <an********@gmail.com> wrote in message
news:3u************@individual.net...
var rules = styleSheet.rules | styleSheet.cssRules;


Or even a question mark colon :-)

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

Thats better,

Aaron
Nov 23 '05 #9

P: n/a
"Aaron Gray" <an********@gmail.com> wrote in message
news:3u************@individual.net...
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
Nov 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.