13

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 :]

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
eric
  • 454
  • 2
  • 9
  • 21
  • I don't really think you're allowed to do this. This might be restricted due to security restrictions. – s3v3n Apr 10 '11 at 04:05
  • this was more of an example I gave a second one in a comment on the answer submitted. I just really need a way to simulate a mouse click on something. – eric Apr 10 '11 at 04:14
  • 1
    have you tried `$('#your_elem').trigger('click')` on page load? (jQuery) – tradyblix Apr 10 '11 at 06:58
  • yeah I have it doesn't work I am trying to capture the x-webkit-speech object and seeing if I can trigger/change its state – eric Apr 10 '11 at 17:02

2 Answers2

3

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.

Jeffery To
  • 11,836
  • 1
  • 27
  • 42
  • Yeah this is what I was worried about... I guess I will wait for further development and just release what I have so far thanks for your help – eric Apr 11 '11 at 03:23
1
<button id="myButton" onClick="alert('You clicked me!');">Click me</button>


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

http://fiddle.jshell.net/Shaz/HgyeZ/

That's with regular clickable items though. But with YouTube Videos, you could also append &autoplay=1 to the end of the url (if it's embedded into a page).

http://fiddle.jshell.net/Shaz/tcMCa/

Shaz
  • 15,637
  • 3
  • 41
  • 59
  • yeah that was just an example another way I could phrase it is if you have an alert popup auto press ok or something like that. I just want to simulate a mouse click. – eric Apr 10 '11 at 04:13
  • You should write a bit about event dispatching, since a solution like this isn't available for most events. –  Apr 10 '11 at 04:18