SetAttribute doesn't work, please help

R

RC

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?
 
V

VK

RC said:
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>
 
L

Laurent Bugnion

Hi,
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
 
L

Laurent Bugnion

Hi,
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
 
R

RC

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.

Hi,
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?
 
L

Laurent Bugnion

Hi,
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top