Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > SetAttribute doesn't work, please help

Reply
Thread Tools

SetAttribute doesn't work, please help

 
 
RC
Guest
Posts: n/a
 
      10-03-2006
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?
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      10-03-2006

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>

 
Reply With Quote
 
 
 
 
Laurent Bugnion
Guest
Posts: n/a
 
      10-03-2006
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
 
Reply With Quote
 
Laurent Bugnion
Guest
Posts: n/a
 
      10-03-2006
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
 
Reply With Quote
 
RC
Guest
Posts: n/a
 
      10-04-2006
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
<(E-Mail Removed)> 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?

 
Reply With Quote
 
Laurent Bugnion
Guest
Posts: n/a
 
      10-04-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DOM help. setAttribute won't work fj Javascript 2 05-18-2005 11:01 PM
Problems using GetAttribute and SetAttribute of the IAttributeAccessor interface with web controls duff@netcomuk.co.uk ASP .Net 3 05-03-2005 01:20 PM
request.setAttribute(...) versus session.setAttribute(...) Matt Java 7 11-08-2004 02:00 PM
Problem with get/setAttribute Steve Peake Java 3 05-09-2004 08:07 PM
ServletRequest.setAttribute question Joerg Gippert Java 2 07-30-2003 11:28 PM



Advertisments