admin管理员组

文章数量:1415100

My iframe content responds to key press, is there a way to send key press into the iframe from the page which contains the iframe?

By the way, the usual way to simulate key press does not work in the scenario:

var ev = $.Event('keypress');
ev.which = 68;
ev.ctrlKey = true;
$('#frame').trigger(ev);

My iframe content responds to key press, is there a way to send key press into the iframe from the page which contains the iframe?

By the way, the usual way to simulate key press does not work in the scenario:

var ev = $.Event('keypress');
ev.which = 68;
ev.ctrlKey = true;
$('#frame').trigger(ev);
Share Improve this question asked Nov 18, 2012 at 7:17 user972946user972946 2
  • 1 i am sure you are aware of Cross Frame Scripting – NullPoiиteя Commented Nov 18, 2012 at 7:22
  • 1 Thank you. Now I see that it is a security concern. You have the right answer. My use case is that I want to send control key and alt key to my shellinabox page from a blackberry phone, which does not have either of those keys. – user972946 Commented Nov 18, 2012 at 7:24
Add a ment  | 

1 Answer 1

Reset to default 3

This is an old question, but I hope this answer is helpful to someone.

There is a new feature in HTML5, window.postMessage, which allows for munication between an iframe and the hosting window. There's some good documentation here.

As usual, you can expect some inconsistencies across browsers: in my experience, Firefox accepts any object as the message, but IE will only accept a string.

There have been some developments to make this feature easy to use in different browsers, including polyfills to make it work in older browsers.

The one that I ended up using was Ben Alman's jquery plugin.

Enjoy!

本文标签: javascriptIs there a way to send keyboard key press to iframe contentStack Overflow