IE6 Ignores JavaScript's return false on a Link

V

vunet.us

Why does IE6 ignores JavaScript's return false on a link and how to
fix it? Firefox works perfect!

<a href="page1.html" onclick="return test(this)">Test</a>

JS:

function test(obj)
{
if(obj.href=='page1.html'){
doSomething();
return false;
}else{
return true;
}
}
 
V

vunet.us

(e-mail address removed) said the following on 4/17/2007 12:29 PM:


IE isn't ignoring the return false, it is returning true. The href
property is an absolute path in IE (test it with an alert).


alert(obj.href)

And you will see that the href property isn't what you expect it to be.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Sorry, I meant this does not work:

function doSomething(){
alert('no go');
}

function test(obj)
{
if(obj.href.toString().indexOf("page1.html")!= -1){
doSomething();
return false;
}else{
return true;
}
}
 
V

vunet.us

(e-mail address removed) said the following on 4/17/2007 3:18 PM:




Define "does not work" as I get the alert and the URL is not followed
with a copy/paste of your code. So you must have something else
(probably in the real doSomething()) that is causing it to break.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

I will test doSomething(), thanks. But the strange this is that it
works with FF, not IE6. I did not test IE7 or other browsers yet.
 
L

Lee

(e-mail address removed) said:

Sorry, I meant this does not work:

function doSomething(){
alert('no go');
}

function test(obj)
{
if(obj.href.toString().indexOf("page1.html")!= -1){
doSomething();
return false;
}else{
return true;
}
}

Have you even considered alerting the value of obj.href before deciding that the
browser doesn't work?


--
 
V

vunet.us

(e-mail address removed) said:




Have you even considered alerting the value of obj.href before deciding that the
browser doesn't work?

--

I have a test case which shows that this function above works in IE6
if used apart from the whole page. Within a page, it does not work and
follows the link (when it shouldn't). I did not have a deeper look yet
but will work on that shortly. Perhaps, in a meantime, anyone may have
any suggestions for this approach: how effective it is and,
considering all other facts, what other alternative may exist.
Thanks.
 
L

Lee

(e-mail address removed) said:
I have a test case which shows that this function above works in IE6
if used apart from the whole page. Within a page, it does not work and
follows the link (when it shouldn't). I did not have a deeper look yet
but will work on that shortly. Perhaps, in a meantime, anyone may have
any suggestions for this approach: how effective it is and,
considering all other facts, what other alternative may exist.

The approach is fine. Either the value of obj.href is not what you
expect, or your doSomething() function contains errors.


--
 
V

vunet.us

(e-mail address removed) said:







The approach is fine. Either the value of obj.href is not what you
expect, or your doSomething() function contains errors.

--

Finally!

I found the problem. I have some included JS file with
document.onclick = somefunction which returned true. Hah! But how did
Firefox get away with it?.. I do not know :)

Thanks everyone!
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top