views:

135

answers:

2

Hi folks,

is it possible to use coordinates in order to simulate a click? Or is this technically impossible? :O

I'm using Javascript within a webpage.


Help would be great, and also quite interesting!!! =)

+2  A: 

For security reasons, you can't move the mouse pointer with javascript, nor simulate a click with it.

What is it that you are trying to accomplish?

quantumSoup
@Aicule: thanks for letting me know! I'll add a bit of info to the question. I'm just experimenting, nothing really productive =)
RadiantHex
+4  A: 

As Aircule answered, you can't simulate a "real" mouse click via JavaScript. However, you can fire an element's click event, and you can get an element using x/y co-ordinates - so you could fire a click event on the element at x/y. Since you tagged jQuery:

$(document.elementFromPoint(x, y)).click();  

https://developer.mozilla.org/En/DOM:document.elementFromPoint

Andy E
That's amazing!! Exactly what I was looking for!!! =O
RadiantHex
does it work with IE and WebKit?
RadiantHex
@RadiantHex: yes, in fact IE was the first to implement it and it goes way back to IE6. Chrome, Firefox and Safari 5 implementations are fine but Safari 4's and Opera's are incorrect (though workable). See http://www.quirksmode.org/dom/w3c_cssom.html#documentview.
Andy E
I'm so happy about this discovery!! =D Makes many things deemed impossible possible now =) ... or at least less complicated. Thanks!!
RadiantHex