Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate real mouse click

What I want to do is execute a mouse click say on youtube to press play when the page loads. How do I click that specific location (assuming it is always in the same location)?

I have tried and failed with

var e = document.getElementById('myelem'); e.click();
var e = new jQuery.Event("click");e.pageX=x;e.pageY=y;$("#elem").trigger(e);

and stuff like that. nothing really works. Any suggestions? I am using google chrome


alright it seems like there has been a little confusion so I will further explain. I have created a popup tied to a keystroke event what I want to do is trigger x-webkit-speech by clicking the microphone that is in my popup so that the user does not have to click it themselves. I have tried a bunch of ways like above and have not been successful. After this my program will be done so I really would love some help thanks :]

like image 357
eric Avatar asked Apr 10 '11 03:04

eric


Video Answer


1 Answers

In general, browsers won't let simulated mouse clicks trigger "real" actions, e.g. a jQuery click() won't cause the browser to follow a link. Otherwise, spammers could trigger banner clicks on every page load (among other more malicious uses).

According to http://www.filosophy.org/2011/03/talking-to-the-web-the-basics-of-html5-speech-input/:

Eventually, it will be possible to invoke the speech-recognition directly with the startSpeechInput() method, but to my knowledge this is not implemented in any of the current browsers.

I suggest waiting for Chrome to implement the API so that you can trigger speech input from JavaScript.

like image 188
Jeffery To Avatar answered Sep 18 '22 23:09

Jeffery To