Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old January 24th, 2007, 12:35 PM
Manfred Kooistra
Guest
 
Posts: n/a
Default 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?

  #2  
Old January 24th, 2007, 12:55 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help

Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:
Quote:
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)
  #3  
Old January 24th, 2007, 01:05 PM
Manfred Kooistra
Guest
 
Posts: n/a
Default 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.

  #4  
Old January 24th, 2007, 01:15 PM
Manfred Kooistra
Guest
 
Posts: n/a
Default 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?

  #5  
Old January 24th, 2007, 01:45 PM
Manfred Kooistra
Guest
 
Posts: n/a
Default 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 ...

  #6  
Old January 24th, 2007, 01:45 PM
Osmo Saarikumpu
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help

Manfred Kooistra wrote:
Quote:
<div id="one" style="display: block;">content</div>
<div id="two" style="display: none;">content</div>
Quote:
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
  #7  
Old January 24th, 2007, 01:55 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help

Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:
Quote:
Thanks, Evertjan. I changed the file to this:
[please always quote on usenet]
Quote:
<html>
<head>
<script language="javascript" type="text/javascript">
//<![CDATA[
Why this CDATA stuff?? Leave it out!!!!!!!!!
Quote:
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)
  #8  
Old January 24th, 2007, 01:55 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help

Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:
Quote:
Sorry, there is an error code.
Quote:
I changed the file to HTML 4 Transitional with:
Thas sould have no influence.
Quote:
<script language="javascript" type="text/javascript">
Do not use language="javascript", it has been deprecated for many yesrs.
Quote:
<!--
Same for <!--

Quote:
function mySwitch() {
document.getElementById('one').style.display = 'none';
document.getElementById('two').style.display = 'block';
>}
// -->
and for // -->
Quote:
</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.
Quote:
Any ideas?
>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
  #9  
Old January 24th, 2007, 01:55 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help

Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:
Quote:
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)
  #10  
Old February 3rd, 2007, 09:45 PM
Manfred Kooistra
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help

On 24 Jan., 14:50, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
Quote:
Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:
>
Quote:
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.

  #11  
Old February 3rd, 2007, 09:45 PM
Manfred Kooistra
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help

On 24 Jan., 14:37, Osmo Saarikumpu <o...@weppipakki.comwrote:
Quote:
Manfred Kooistra wrote:
Quote:
<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
In my file the two divs did not contain the same "content" and
"content" [that was an error here in my posts], but images in one div
and text in the other - so the switching or non-switching was
unmistakeable.

  #12  
Old February 17th, 2007, 01:05 AM
kim mizell
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help



help! i need astince it will not work.

*** Sent via Developersdex http://www.developersdex.com ***
  #13  
Old February 17th, 2007, 04:25 AM
Jim Land
Guest
 
Posts: n/a
Default Re: JavaScript not working, need help

"Manfred Kooistra" <manfred.kooistra@gmx.dewrote in
news:1169641402.567743.228740@l53g2000cwa.googlegr oups.com:
Quote:
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?
>
>
You named your function "switch", which is the name of a JS built-in
function, so JS refuses to work. Rename your function something else,
like "switchthediv" and it works OK in FF and IE.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.