0

I can bind eventsin the current window, but I'm using TinyMCE which creates an iframe, and I want to be able to set a keybind to the iframe window to capture a control-S

I have:

$(window.child).keydown(function(e) {
    if(!args) args=[]; // IE barks when args is null
    if(e.keyCode == key.charCodeAt(0) && e.metaKey) {
        callback.apply(this, args);
        return false;
    }
});

But that isn't working. Ideas?

Thanks

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

2 Answers2

0
$( document.iframe1.contentWindow.document).keydown(...);

iframe1 is an iframe

Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
0

It's at first a problem of timing, when to access the iframe.

If you bind it somewhere inside the parent document, it may be that the document inside the iframe isn't loaded yet. So I would suggest to assign it directly to the onload-event of the iframe, to be sure that it is accessable. Furthermore it depends on the browser, how you'll get a to pointer the document:

<iframe onload="fx(this)" src="some.html" ></iframe>
<script  type='text/javascript'>
function fx(win)
{ 
  var doc=win.contentDocument||win.contentWindow.document;
  $(doc).keypress(
      function(event)
      {
        alert(event.charCode||event.keyCode);
      });
}
</script>
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201