Click to See Complete Forum and Search --> : IE: Stopping Link Navigation with Dynamic Event Handler


DavidM
04-19-2006, 11:13 PM
Hi all. I could use some help. I've spent quite a while looking online for a solution but I got in over my head.

I've written a script that goes through a document and assings an onclick event to all the links in a page. The new function assigned basically calls a seperate function and then: return false;

The goal was that the return false would allow for my function to fire, but prevent the browser from actually following the link.

This works in FireFox, however in IE the return false is not preventing the navigation to the url in the link.

I've read up on this and I think I get why it's not working, I'm just not clear on if there is a work around or not.

Is there anyway to assing an onclick in runtime to an a tag element, and prevent IE from actually following that href?

Thanks in advance,
- Dave

felgall
04-20-2006, 01:36 AM
onclick="yourfunction();return false"

OR

onclick="return yourfunction()" (provided that yourfunction() returns false)

DavidM
04-20-2006, 06:56 AM
Unfortunately that doesnt work. That works if you put that line in the in a tag within the html document ... but when you do this at runtime it doesnt. The return false no longer works in IE.

Also, apparently you can't assign onclick in that way in runtime. You cant give it a string value, you have to give it a function

atag.onlick = function { mycoolfunction();return false; }

the problem being that for some reason return false doesnt do anything in IE anymore.

DavidM
04-20-2006, 04:41 PM
Has anyone been able to stop IE from navigating a link that has been clicked using an event handler that was added at run time?

Using the above seems to work if it was coded into the html doc, but if its added after the page loads at runtime, return false just doesnt do the trick, :(

Orc Scorcher
04-21-2006, 02:32 AM
Are you sure yourcoolfunction does not give an error so that the 'return false' statement is never executed? atag.onclick = function() { return false } works just fine for me. As an alternative, you could try thisatag.onclick = function() {
if (window.event) event.returnValue = false
yourcoolfunction()
}

DavidM
04-22-2006, 10:29 AM
none of that is working here. :confused:

Here is the code I'm working off of (with examples). If you load it in FireFox it works as expected, however in IE it does not.

For whatever reason, the return false is doing nothing in IE.

http://www.davidmeade.com/wip/media_test.php

Any help is greatly appreciated

Orc Scorcher
04-22-2006, 11:27 AM
IE doesn't like your attempt to appendChild an embed element to an object element. You could wrap the appendChild in a try-catch block if you don't mind that's not compatible with IE 5.0.

DavidM
04-22-2006, 12:22 PM
Orc I could kiss you! :D

It looks like I'm on my way to having a fairly cross-browser-friendly script!

THANK YOU!