SetAttribute doesn't work, please help

Discussion in 'Javascript' started by RC, Oct 3, 2006.

  1. RC

    RC Guest

    Please look at this page: http://www.norbertverboeket.com/test

    There are 2 stylesheets, StyleSheet.css and BigStyle.css. One is
    copied from the other. Only the font-size is changed, so there
    couldn't be anything wrong with the copy.

    When the user clicks on 'Click here', the function SwitchSheets should
    switch the stylesheets:
    document.all["ScreenStyle"].SetAttribute("HREF", "BigStyle.css",0);

    The only thing I get is 'error on page'.

    I have IE 6. What am I doing wrong here?
    RC, Oct 3, 2006
    #1
    1. Advertising

  2. RC

    VK Guest

    RC wrote:
    > Please look at this page: http://www.norbertverboeket.com/test


    > When the user clicks on 'Click here', the function SwitchSheets should
    > switch the stylesheets:
    > document.all["ScreenStyle"].SetAttribute("HREF", "BigStyle.css",0);
    >
    > The only thing I get is 'error on page'.
    >
    > I have IE 6. What am I doing wrong here?


    JavaScript/JScript is case-sensitive and the relevant method name is
    called setAttribute (small "s"). Be very careful with MSDN samples for
    JScript: they are plain of case typos as some sections seem to be made
    by pure VB guys who missed to switch the mind on time. Some parts are
    really funny in this matter, like say FileSystemObject specs: here
    proper case/ case mishmash are going from method to method
    alphabetically. Evidently it was made by two people with C++ practice
    and VB practice respectively, so the correcteness depended on who came
    back from the coffee break. :)

    Also document.all is IE-specific collection. The rest of the civilized
    word :) is using document.getElementById method.

    Here is a working sample for both IE and Firefox (you need sheet1.css
    and sheet2.css with different rules for P element to see the effect):

    <html>
    <head>
    <title>CSS Switch</title>
    <meta http-equiv="Content-Type"
    content="text/html; charset=iso-8859-1">

    <link id="mySheet" rel="stylesheet" href="sheet1.css">

    <script type="text/javascript">
    function changeSheet() {
    var mySheet = document.getElementById('mySheet');
    if (('undefined' == typeof mySheet.main) || (mySheet.main)) {
    mySheet.href = 'sheet2.css';
    mySheet.main = false;
    }
    else {
    mySheet.href = 'sheet1.css';
    mySheet.main = true;
    }
    }
    </script>
    </head>

    <body>
    <p>Sample</p>
    <button type="button" onClick="changeSheet()">Change
    stylesheet</button>
    </body>
    </html>
    VK, Oct 3, 2006
    #2
    1. Advertising

  3. Hi,

    RC wrote:
    > Please look at this page: http://www.norbertverboeket.com/test
    >
    > There are 2 stylesheets, StyleSheet.css and BigStyle.css. One is
    > copied from the other. Only the font-size is changed, so there
    > couldn't be anything wrong with the copy.
    >
    > When the user clicks on 'Click here', the function SwitchSheets should
    > switch the stylesheets:
    > document.all["ScreenStyle"].SetAttribute("HREF", "BigStyle.css",0);


    document.all is IE only, deprecated, and should be avoided like the
    plague if you ask me. document.getElementById is the way to go. But
    that's not the cause for the error.

    JavaScript is case sensitive, and follows the Java naming conventions.
    Use camel notation, not pascal notation:

    setAttribute( ... );

    Note also that you should check if the result of
    document.getElementById( ... ) is null.

    HTH,
    Laurent

    >
    > The only thing I get is 'error on page'.
    >
    > I have IE 6. What am I doing wrong here?


    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Oct 3, 2006
    #3
  4. Hi,

    VK wrote:
    > JavaScript/JScript is case-sensitive and the relevant method name is
    > called setAttribute (small "s"). Be very careful with MSDN samples for
    > JScript: they are plain of case typos as some sections seem to be made
    > by pure VB guys who missed to switch the mind on time.


    FileSystemObject uses the Pascal notation. The documentation is correct
    in that matter.

    HTH
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Oct 3, 2006
    #4
  5. RC

    RC Guest

    Thanks Laurent, that was it. I have it from the 'Developing web
    applications with Microsoft Visual Basic.Net and c#' book, so I guess
    you're not surprised that I used this 'Microsoft' Java code. But
    anyway, I'll try to avoid it next time.

    On Tue, 03 Oct 2006 23:08:15 +0200, Laurent Bugnion
    <> wrote:

    >Hi,
    >
    >RC wrote:
    >> Please look at this page: http://www.norbertverboeket.com/test
    >>
    >> There are 2 stylesheets, StyleSheet.css and BigStyle.css. One is
    >> copied from the other. Only the font-size is changed, so there
    >> couldn't be anything wrong with the copy.
    >>
    >> When the user clicks on 'Click here', the function SwitchSheets should
    >> switch the stylesheets:
    >> document.all["ScreenStyle"].SetAttribute("HREF", "BigStyle.css",0);

    >
    >document.all is IE only, deprecated, and should be avoided like the
    >plague if you ask me. document.getElementById is the way to go. But
    >that's not the cause for the error.
    >
    >JavaScript is case sensitive, and follows the Java naming conventions.
    >Use camel notation, not pascal notation:
    >
    >setAttribute( ... );
    >
    >Note also that you should check if the result of
    >document.getElementById( ... ) is null.
    >
    >HTH,
    >Laurent
    >
    >>
    >> The only thing I get is 'error on page'.
    >>
    >> I have IE 6. What am I doing wrong here?
    RC, Oct 4, 2006
    #5
  6. Hi,

    RC wrote:
    > Thanks Laurent, that was it. I have it from the 'Developing web
    > applications with Microsoft Visual Basic.Net and c#' book, so I guess
    > you're not surprised that I used this 'Microsoft' Java code. But
    > anyway, I'll try to avoid it next time.


    It's not Java, it's JavaScript. Very different animal, though they use
    similar notations.

    Greetings,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Oct 4, 2006
    #6
    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. Matt
    Replies:
    7
    Views:
    14,741
    John C. Bollinger
    Nov 8, 2004
  2. setAttribute() does not work

    , Aug 22, 2003, in forum: Javascript
    Replies:
    3
    Views:
    154
  3. Shea Martin

    Win32::File::SetAttribute does not work

    Shea Martin, Oct 12, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    286
    Shea Martin
    Oct 13, 2005
  4. ItsMillerTime4u
    Replies:
    1
    Views:
    490
    Randy Webb
    Mar 4, 2005
  5. fj
    Replies:
    2
    Views:
    133
Loading...

Share This Page