Forum Moderators: open

Message Too Old, No Replies

Variable undefined. Help a JS newbie.

         

mattr555

9:04 am on Feb 22, 2005 (gmt 0)

10+ Year Member



I am pretty competant with HTML, CSS, and ASP but have managed to avoid JS for too many years. Decided to bite the bullet and try and teach myself but finding the going tough.

Could someone tell me why the following doesnt work. IE reports ""jstest" undefinded" and Firefox does nothing.

I have 2 external style sheets and i am attempting to use JS to change the href attribute of the link tag to point to the other .css file.

Thanks.

<html>
<head>
<title>DOM Scripting Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link id="stylelink" rel="stylesheet" type="text/css" href="jstest.css" media="screen" />

<script type="text/javascript">

function switchsheet(whichsheet){
mysheet=whichsheet;
document.getElementById("stylelink").setAttribute("href",mysheet+".css");

}
</script>

</head>
<body>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed dictum augue at wisi. Donec nec odio. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Pellentesque venenatis vulputate elit. Nulla facilisi. Nunc mattis, tortor a porta rhoncus, metus massa nonummy orci, eu gravida nibh nunc a wisi. Nunc mollis fringilla enim. Nullam imperdiet massa ac elit. Vivamus semper risus ut nunc. Fusce aliquam interdum nisl. Pellentesque gravida eros in felis. Duis sit amet lectus eu nunc pharetra iaculis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>

<a href="#" onclick="switchsheet(jstest2)">click here for style sheet 2</a>
<br />
<a href="#" onclick="switchsheet(jstest)">click here for style sheet 1</a>

</body>
</html>

Alternative Future

10:33 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello mattr555,

When passing a parameter to a function it should be quoted like so:

<a href="javascript:switchsheet('jstest2')">click here for style sheet 2</a>
<br />
<a href="javascript:switchsheet('jstest')">click here for style sheet 1</a>

HTH,

-George

edit: removed onclick function and replaced href with call to javascript

mattr555

10:42 am on Feb 22, 2005 (gmt 0)

10+ Year Member



Thanks alot. It's always the simple things.

Do you have a simple explanation of the difference between DHTML and DOM Scripting?

Alternative Future

10:45 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sent you a sticky with a link that might be helpful.

-George

mattr555

10:53 am on Feb 22, 2005 (gmt 0)

10+ Year Member



Thanks for the link.

Going back to my original query. Is it better to point the href to the javascript instead of using an event or is it just personal preference?

Alternative Future

11:08 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is just personal preference, I am sure there are some reasons that might be pointed out to you by the more experienced JavaScript gurus here.

-George

cunningmark

11:34 am on Feb 22, 2005 (gmt 0)

10+ Year Member



Style sheet switcher.

I use this to increase the font size for the page, so that the site becomes parcially sited freindly.

In the head:
<link rel="stylesheet" type="text/css" href="standard.css" title="standard" />
<link rel="alternate stylesheet" type="text/css" href="large.css" title="large" />

In body:
<a style='cursor:hand' onclick='parent.header.setActiveStyleSheet("large");parent.main.focus()'><img src="images/fontsize.gif" border="0"></a>

I was using this in a Frameset, button refocuses the action to the main part of the page.

This is a one way action, as that was all I needed at the time, but i have seen other scripts which you can either pick the style sheet, or switch between the style sheets.

Google search 'HTML CSS Style sheet switch' and trawl through the results.