/    Sign up×
Community /Pin to ProfileBookmark

IE: Stopping Link Navigation with Dynamic Event Handler

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

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@felgallApr 20.2006 — onclick="yourfunction();return false"

OR

onclick="return yourfunction()" (provided that yourfunction() returns false)
Copy linkTweet thisAlerts:
@DavidMauthorApr 20.2006 — 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.
Copy linkTweet thisAlerts:
@DavidMauthorApr 20.2006 — 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, ?
Copy linkTweet thisAlerts:
@Orc_ScorcherApr 21.2006 — Are you sure yourcoolfunction does not give an error so that the 'return false' statement is never executed? [i]atag.onclick = function() { return false }[/i] works just fine for me. As an alternative, you could try thisatag.onclick = function() {
if (window.event) event.returnValue = false
yourcoolfunction()
}
Copy linkTweet thisAlerts:
@DavidMauthorApr 22.2006 — none of that is working here. ?

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.

[URL=http://www.davidmeade.com/wip/media_test.php]http://www.davidmeade.com/wip/media_test.php[/URL]

Any help is greatly appreciated
Copy linkTweet thisAlerts:
@Orc_ScorcherApr 22.2006 — 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.
Copy linkTweet thisAlerts:
@DavidMauthorApr 22.2006 — Orc I could kiss you! ?

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

THANK YOU!
×

Success!

Help @DavidM spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...