Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between .click() and creating a mouse event?

So I am trying to work out the differences between

link.click()

and

var event = document.createEvent("MouseEvents");
event.initEvent("click", true, false);
link.dispatchEvent(event);

As far as I can tell these should be the same things (however working with my jsfiddle example of exporting a csv from a URI this is not the case as they perform differently from browser to browser)

Using .click() with firefox the popup to download the csv will not show (it will in chrome)

see example -> http://jsfiddle.net/a5E9m/23/

Where as using the Mouse events it will

see example -> http://jsfiddle.net/a5E9m/25/

like image 974
Jesse Whitham Avatar asked Jan 06 '14 03:01

Jesse Whitham


1 Answers

I think that Firefox has restrictions around the click function on an <a> element. See here. Whereas when you wire up the mouse event yourself, you are manually adding the click wiring. Also, see here and here.

Also, as Boris Zbarsky pointed out in the comments, the <a> element does not have a click function on it in the spec.

like image 181
acarlon Avatar answered Oct 19 '22 12:10

acarlon