Forum Moderators: open
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!
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.
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.