Javascript: click() not navigating to the link in Firefox 2

786 Views Asked by At

While following the answer here: Click() works in IE but not Firefox

I no longer get the "click is not a function message" error message and indeed get the "Clicked" alert message, however the browser does not navigate to the page. I tried it on the latest version of firefox and it navigates, just not happening in Firefox 2.

HTMLElement.prototype.click = function() {var evt = 
this.ownerDocument.createEvent('MouseEvents');evt.initMouseEvent('click', true, true, 
this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, 
null);this.dispatchEvent(evt);};

document.onclick= function(event) { if (event===undefined) event= window.event; var target= 
'target' in event? event.target : event.srcElement; alert("clicked");};

document.getElementById("anId").click();

document.onclick= function(event) { if (event===undefined) event= window.event; var target= 
'target' in event? event.target : event.srcElement; alert("clicked");};
5

There are 5 best solutions below

6
On

Use document.getElementById("anId").onclick(); it will work on all the browsers. click(); works only on IE.

0
On

This question has already been answered here.

Here's what you're to use to add the functionality...

HTMLElement.prototype.click = function() {
   var evt = this.ownerDocument.createEvent('MouseEvents');
   evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
   this.dispatchEvent(evt);
}
0
On

I think since FF2 is too old and not sure whether it supports the click prototype, better way would be to something like this

html

<a id="link" href="url">click here</a>

js

$(document).ready(function(){
    $("#link").click(function(){
        window.location.href = $(this).attr('href');
    });
});

also if this is somethng you don't want to do theb also try something like this:

$(document).ready(function(){
        $("#link")[0].click();
    });
0
On

Try to use JQuery... http://api.jquery.com/click/ maybe you are trying to listen on an html element that does not expect to accept onClick event. click() only works in IE. and if you gonna try Jquery event listener click, you can cover all the problems in all major browsers

0
On

You may try jQuery plugin and use it's .click() event.

You can see more details here.

Hope this helps.. :)