最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to send keyboard key press to iframe content? - Stack Overflow

programmeradmin1浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论