Decoded Personalized Google code

Discussion in 'Javascript' started by Aaron Gray, Nov 17, 2005.

  1. Aaron Gray

    Aaron Gray Guest

    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
    Aaron Gray, Nov 17, 2005
    #1
    1. Advertising

  2. Aaron Gray

    Aaron Gray Guest

    Re: Decoded Personalized Google code - function SetClassStyle( clazz, style, value)

    "Aaron Gray" <> wrote in message
    news:...
    > 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
    Aaron Gray, Nov 17, 2005
    #2
    1. Advertising

  3. Aaron Gray

    Aaron Gray Guest

    Re: Decoded Personalized Google code - Is "var a = b | c;" portable ?

    "Aaron Gray" <> wrote in message
    news:...
    > "Aaron Gray" <> wrote in message
    > news:...
    >> 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
    >
    >
    >
    Aaron Gray, Nov 17, 2005
    #3
  4. Aaron Gray

    Aaron Gray Guest

    Re: Decoded Personalized Google code -- Is "var a = b | c;" portable ?

    > var rules = styleSheet.rules | styleSheet.cssRules;

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

    Aaron
    Aaron Gray, Nov 17, 2005
    #4
  5. Aaron Gray

    Matt Kruse Guest

    Re: Decoded Personalized Google code - function SetClassStyle( clazz, style, value)

    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
    Matt Kruse, Nov 17, 2005
    #5
  6. Aaron Gray

    Aaron Gray Guest

    Re: Decoded Personalized Google code - function SetClassStyle( clazz, style, value)

    "Matt Kruse" <> wrote in message
    news:...
    > 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
    Aaron Gray, Nov 17, 2005
    #6
  7. Aaron Gray

    Aaron Gray Guest

    Re: Decoded Personalized Google code -- Is "var a = b | c;" portable ?

    "Aaron Gray" <> wrote in message
    news:...
    >> 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
    Aaron Gray, Nov 17, 2005
    #7
  8. Aaron Gray

    Aaron Gray Guest

    Re: Decoded Personalized Google code - getElementById()

    "Aaron Gray" <> wrote in message
    news:...
    > 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
    Aaron Gray, Nov 17, 2005
    #8
  9. Aaron Gray

    Aaron Gray Guest

    Re: Decoded Personalized Google code -- Is "var a = b | c;" portable ?

    "Aaron Gray" <> wrote in message
    news:...
    >> var rules = styleSheet.rules | styleSheet.cssRules;

    >


    Or even a question mark colon :)

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

    Thats better,

    Aaron
    Aaron Gray, Nov 17, 2005
    #9
  10. Aaron Gray

    Aaron Gray Guest

    "Aaron Gray" <> wrote in message
    news:...
    > 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
    Aaron Gray, Nov 17, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. T Conti

    Error decoded 8 bit characters.

    T Conti, Apr 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    362
    Joerg Jooss
    Apr 11, 2004
  2. Ben Amada

    URL encoded text automatically decoded?

    Ben Amada, Oct 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    1,111
    Mr Newbie
    Oct 4, 2005
  3. pekka niiranen

    Is this a bug? BOM decoded with UTF8

    pekka niiranen, Feb 10, 2005, in forum: Python
    Replies:
    10
    Views:
    595
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Feb 12, 2005
  4. Roland Rickborn

    wxPython: Icon aus base64 decoded Image

    Roland Rickborn, Dec 11, 2006, in forum: Python
    Replies:
    1
    Views:
    452
    Bjoern Schliessmann
    Dec 11, 2006
  5. Jacob Andersen

    Validator's ErrorMessage is decoded

    Jacob Andersen, Jun 24, 2010, in forum: ASP .Net
    Replies:
    3
    Views:
    1,004
    Sarath Babu S
    Jul 8, 2010
Loading...

Share This Page