-1

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");};
Community
  • 1
  • 1
KJW
  • 15,035
  • 47
  • 137
  • 243
  • 3
    I don't use ff2 browser but the library I use, incorporates xulrunner from FF2. – KJW Mar 01 '12 at 08:09
  • Check these urls, I hope they help you :) http://forums.asp.net/t/1321236.aspx/1 and http://www.devtoolshed.com/content/fix-firefox-click-event-issue – Mhmd Mar 07 '12 at 16:38
  • provide html of your anchor link and also check in firebug console .is there any error on click – sandeep Mar 08 '12 at 06:57
  • yeah ive looked at those and it seems like theres just no way to go about doing this in such an old ff engine. – KJW Mar 08 '12 at 07:11

5 Answers5

1

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

Taha
  • 1,086
  • 1
  • 10
  • 20
  • it means there's some other bug. Either no element exists with this `id` or that element doesn't have `onclick` attribute defined. – Taha Mar 01 '12 at 08:18
  • manually inject onclick attribute to the element with empty value? – KJW Mar 01 '12 at 08:19
  • 1
    this method requires the element to have onclick attribute, I need a solution that will generate a click event on any element. – KJW Mar 01 '12 at 08:40
  • like a scenario I would ave problem with is, if you were to do href.location = "somewhere"; onclick(), however, I don't want to override the default onclick behavior set by the web application. What I am looking for is a way to emulate mouse click behavior using javascript in FF2....there seems to be no other way. – KJW Mar 01 '12 at 19:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8433/discussion-between-tsf-and-kim-jong-woo) – Taha Mar 02 '12 at 09:16
  • are you trying to listen an onclick event on a element that doesn't expect to have onclick event? – Netorica Mar 08 '12 at 06:44
1

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();
    });
Abhidev
  • 7,063
  • 6
  • 21
  • 26
1

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

You can see more details here.

Hope this helps.. :)

Alfred
  • 21,058
  • 61
  • 167
  • 249
0

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);
}
Community
  • 1
  • 1
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
0

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

Netorica
  • 18,523
  • 17
  • 73
  • 108