Why does the onclick function run on onclick, but not when I call it manually?
Posted
by
Codemonkey
on Stack Overflow
See other posts from Stack Overflow
or by Codemonkey
Published on 2011-01-11T12:30:15Z
Indexed on
2011/01/11
13:53 UTC
Read the original article
Hit count: 147
JavaScript
|html
This is my code
// Register onclick
var old_onclick = link.onclick;
link.onclick = function() {
astr_track_action(param);
if(typeof(old_onclick) == "function")
old_onclick();
}
And this is the html
<a onclick="alert('hello!');" href="http://www.google.com?acme=link&foo=bar">To google!</a>
When I click the link, the alert pops up. But when I override the onclick with my JS code, the alert does not pop up.
Any ideas?
Edit: I just want to add, I have debugged and confirmed that old_onclick()
is run, but no alert message shows up.
Edit: Here is the full code from the loop start. I don't see how it's relevant, but it was requested:
for(var i = 0; i < document.links.length; i++)
{
var link = document.links[i];
var eventlink = link.href.split("acme=");
if(eventlink.length > 1)
{
var param = eventlink[1].split("&")[0];
var newlink = link.href;
// Register onclick
var old_onclick = link.onclick;
link.onclick = function() {
astr_track_action(param);
if(typeof(old_onclick) == "function")
old_onclick();
}
© Stack Overflow or respective owner