Welcome to WebmasterWorld Guest from 34.231.21.123

Forum Moderators: open

Message Too Old, No Replies

"return false;" not working under Mozilla 1.6a

But fine in IE6 and Opera 7.21

     
11:11 pm on Jan 7, 2004 (gmt 0)

New User

10+ Year Member

joined:Aug 24, 2003
posts:34
votes: 0


Hi WW,

I have a link which looks like this:

<a href="image.jpg" onclick="return function('image.jpg');" target="_blank">test</a>

(note: generic terms in example link above - the function is not actually called 'function')

The function returns false to prevent the browser following the link in the href. While the javascript uses the variable(s) passed to display an appropriate image on the current page, in a designated area.

As the subject of this messages reads, I have no problems with it in IE or Opera. However, Mozilla is not playing ball. It opens the url in a new window, completely ignoring the 'return false'. I have even tried explicitly stating return false inside the onclick code but still no joy :(

Despite hours of searching WW and the Internet I can't find any reason for this. I see most people tend to use '#' in the href which would kind of work but I also want to support non-javascript browsers so this is a no-go.

Any help would be greatly appreciated, it's driving me potty!

9:40 pm on Jan 8, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:Sept 4, 2002
posts:508
votes: 0


Hi crxchaos,

When you're explicitly putting the "return false" into the onclick, are you doing it like this?

<a href="image.jpg" onclick="return false;function('image.jpg');" target="_blank">test</a>

If not, you might give it a shot. If so, hopefully someone smarter comes along. :)

11:50 pm on Jan 8, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 8, 2004
posts:162
votes: 0


Crxchaos,

Perhaps you are looking for something like this?

<a href="javascript:void(0)" onclick="myfunction()">clickme</a>

No need for a return false if you use this code. javascript:void(0) prevents the link from being followed.

ajkimoto

3:34 am on Jan 9, 2004 (gmt 0)

New User

10+ Year Member

joined:Aug 24, 2003
posts:34
votes: 0


garann wrote:

When you're explicitly putting the "return false" into the onclick, are you doing it like this?

<a href="image.jpg" onclick="return false;function('image.jpg');" target="_blank">test</a>

No, I was doing it like this:

<a href="image.jpg" onclick="function('image.jpg');return false;" target="_blank">test</a>

Otherwise the function would not be executed. Interestingly though, when I do put the 'return false' before the function call it successfully prevents the href opening.

/me thinks about the implications of that last sentance for a moment... :o

Ah, I just found a bug in my function! I even slept on this one before posting here because a new day usually helps to find the problem but it didn't help. Turns out I needed a couple of fresh minds to kick start me :)

Thanks for your replies, guys.

4:50 pm on Jan 9, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:Sept 4, 2002
posts:508
votes: 0


Otherwise the function would not be executed. Interestingly though, when I do put the 'return false' before the function call it successfully prevents the href opening.

Glad you caught the bug in your code. As you found out, putting "return false;" first doesn't prevent any JavaScript that comes after it in your event handler from being called. "return false;" is the response your element gives to a mouse event - it does nothing. Once its given that response, it goes on to execute the rest of your orders.