2

Is it possible to simulate a keypress when I click a div? Something like:

$("mydiv").click(function(){
 Presskey->F5
});

Actually the key I'm trying to simulate is F11, so people could press the div and the window would fullscreen. I've searched this and I know it's not possible to make window fullscreen without the user "permission" but in this case he would press the div as he pressed the key. The fact is not everyone knows about F11 (I know I could always put "Press F11 for fullscreen"). Thanks'

alvaro
  • 25
  • 2
  • 5

3 Answers3

4

Yes you can simulate F11 being pressed, but you can't simulate what that does, which is a native browser function (as it would only have an effect in the JavaScript VM area), and not one triggered by JavaScript.

"Press F11 for fullscreen" is your best option here, think of the security issues if you could trigger any browser behavior from JavaScript, e.g. Ctrl+S, etc.

As an aside, this is true even in other areas, for example you can simulate a click event on an anchor and it'll trigger all the JavaScript goodness hooked up to it...but it won't actually follow the link, some (most) things in a browser happen above the JavaScript level, where you don't have access to anything.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • "but it won't actually follow the link" - that example is not true. You can simulate a `click` like this: `$('a')[0].click()` and it will open the link. – Ignitor Sep 13 '12 at 17:06
  • @Ignitor - no...it won't, you can test and see that it won't here: http://jsfiddle.net/tJRNf/ There are very serious security reasons this isn't allowed. – Nick Craver Sep 13 '12 at 19:45
  • Working fine here with Opera 12.02, Firefox 15.0, Chrome 21.0 and IE 9 on Windows (well, you have to change the href to something different than google because google doesn't allow to be loaded in an iframe; but then it works without problems). About the security reasons: you can easily change the page by changing `window.location` so why should the browser prevent this when calling `click()` on a link? – Ignitor Sep 14 '12 at 12:44
1

You cannot programmatically enforce fullscreen, nor can you with keypresses simulate actions which aren't otherwise accessible through JavaScript.

If you are working with HTML video, you may want to have a look at this, which is not cross-browser supported.

Niklas
  • 29,752
  • 5
  • 50
  • 71
1

There is a security issue with this as it can be used by some malicious scripts to exploit it as a method to confuse user and make them do some things that they are not supposed to do... or forcing them to one website ( remember that most of web users aren't that smart and 70% of them are just stupid)