Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   JavaScript not working, need help (http://www.velocityreviews.com/forums/t929401-javascript-not-working-need-help.html)

Manfred Kooistra 01-24-2007 12:23 PM

JavaScript not working, need help
 
I have a problem with some JavaScript code not working. I'm sure I've
done something obviously stupid, but I can't for the live of me figure
it out. Can someone please help?

This is the XHTML document (stripped to the relevant parts):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="switch.js"></script>
</head>
<body>
<div id="one" style="display: block;">content</div>
<div id="two" style="display: none;">content</div>
<div><a href="#" onclick="switch(); return false;">Switch</a></div>
</body>
</html>

and this is the JavaScript file switch.js:

function switch() {
document.getElementById('one').style.display = 'none';
document.getElementById('two').style.display = 'block';
}

Can you see the problem?


Evertjan. 01-24-2007 12:45 PM

Re: JavaScript not working, need help
 
Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

> I have a problem with some JavaScript code not working. I'm sure I've
> done something obviously stupid, but I can't for the live of me figure
> it out. Can someone please help?
>
> This is the XHTML document (stripped to the relevant parts):
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
> <head>
> <script type="text/javascript" src="switch.js"></script>
> </head>
> <body>
> <div id="one" style="display: block;">content</div>
> <div id="two" style="display: none;">content</div>
> <div><a href="#" onclick="switch(); return
> false;">Switch</a></div>
> </body>
> </html>
>
> and this is the JavaScript file switch.js:
>
> function switch() {
> document.getElementById('one').style.display = 'none';
> document.getElementById('two').style.display = 'block';
>}
>
> Can you see the problem?


Do not start with a js in a separate file,
you do not even know if it is loaded.
Do not use tabs on usenet.
Do not the XML crap that is not partinent to your Q.
Do not say "not working" but show us the errorcode and errorline number.

And you/we will see, that "switch" must be a reserved word.

Try:

function mySwitch() {



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Manfred Kooistra 01-24-2007 12:59 PM

Re: JavaScript not working, need help
 
Thanks, Evertjan. I changed the file to this:

<html>
<head>
<script language="javascript" type="text/javascript">
//<![CDATA[
function mySwitch() {
document.getElementById('one').style.display = 'none';
document.getElementById('two').style.display = 'block';
}
//]]>
</script>
</head>
<body>
<div id="one" style="display: block;">content</div>
<div id="two" style="display: none;">content</div>
<div><a href="#" onclick="mySwitch(); return false;">Switch</a></div>
<body>
</html>

There is no error message, it simply does nothing.


Manfred Kooistra 01-24-2007 01:07 PM

Re: JavaScript not working, need help
 
Sorry, there is an error code.

I changed the file to HTML 4 Transitional with:

<script language="javascript" type="text/javascript">
<!--
function mySwitch() {
document.getElementById('one').style.display = 'none';
document.getElementById('two').style.display = 'block';
}
// -->
</script>

and the error message tells me that "mySwitch is not defined" and point
me to line 1, which reads: "<html>".

Any ideas?


Manfred Kooistra 01-24-2007 01:33 PM

Re: JavaScript not working, need help
 
Changes everything back to XHTML, and now it works fine. Strange. I
still have no idea, what the problem was ...


Osmo Saarikumpu 01-24-2007 01:37 PM

Re: JavaScript not working, need help
 
Manfred Kooistra wrote:

> <div id="one" style="display: block;">content</div>
> <div id="two" style="display: none;">content</div>


> There is no error message, it simply does nothing.


It does, but it's not obvious because both divs have the same content.
Change the contents e.g.:

<div id="one" style="display: block;">content 1</div>
<div id="two" style="display: none;">content 2</div>

and you'll see some difference.

--
Osmo

Evertjan. 01-24-2007 01:43 PM

Re: JavaScript not working, need help
 
Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

> Thanks, Evertjan. I changed the file to this:


[please always quote on usenet]

> <html>
> <head>
> <script language="javascript" type="text/javascript">
> //<![CDATA[


Why this CDATA stuff?? Leave it out!!!!!!!!!

> function mySwitch() {
> document.getElementById('one').style.display = 'none';
> document.getElementById('two').style.display = 'block';
>}
> //]]>
> </script>
> </head>
> <body>
> <div id="one" style="display: block;">content</div>
> <div id="two" style="display: none;">content</div>
> <div><a href="#" onclick="mySwitch(); return false;">Switch</a></div>
> <body>
> </html>
>
> There is no error message, it simply does nothing.
>


It would SHOW(!!!) nothing as "content" is the same as "content"!

Try this:

=========== test.html ================
<div id="one" style="display: block;">content: 1</div>
<div id="two" style="display: none;">content: 2</div>

<a href="#" onclick="mySwitch(); return false;">Switch</a>

<script type='text/javascript'>
function mySwitch() {
document.getElementById('one').style.display = 'none';
document.getElementById('two').style.display = 'block';
}
</script>
=======================================

You see you do NOT need any header for this testing,
at least in IE and FF.

Preferably, IMHO, use a button,
an anchor is for linking:

<button onclick='mySwitch();'>Switch</button>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Evertjan. 01-24-2007 01:47 PM

Re: JavaScript not working, need help
 
Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

> Sorry, there is an error code.


> I changed the file to HTML 4 Transitional with:


Thas sould have no influence.

> <script language="javascript" type="text/javascript">


Do not use language="javascript", it has been deprecated for many yesrs.

> <!--


Same for <!--


> function mySwitch() {
> document.getElementById('one').style.display = 'none';
> document.getElementById('two').style.display = 'block';
>}
> // -->


and for // -->

> </script>
>
> and the error message tells me that "mySwitch is not defined" and point
> me to line 1, which reads: "<html>".


So, you have made an unrelated error in your code, Manfred.

> Any ideas?
>




--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Evertjan. 01-24-2007 01:50 PM

Re: JavaScript not working, need help
 
Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

> Changes everything back to XHTML, and now it works fine. Strange. I
> still have no idea, what the problem was ...
>


Again:

1 Please always quote on usenet, this is not email,
others want to read it too,
and not all news servers are current with older mails of a thread.

2 Please share the code [minimal] that maks the difference.
Why make us guess?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Manfred Kooistra 02-03-2007 09:34 PM

Re: JavaScript not working, need help
 
On 24 Jan., 14:50, "Evertjan." <exjxw.hannivo...@interxnl.net> wrote:
> Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:
>
> > Changes everything back to XHTML, and now it works fine. Strange. I
> > still have no idea, what the problem was ...

>
> Again:
>
> 1 Please always quote on usenet, this is not email,
> others want to read it too,
> and not all news servers are current with older mails of a thread.
>
> 2 Please share the code [minimal] that maks the difference.
> Why make us guess?
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)


The solution to my problem is what I posted in my original question.
As I wrote: Now it works, I don't know why. There is no difference, so
I thought there was no reason to post the same code again.



All times are GMT. The time now is 06:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.