views:

23

answers:

2

How can i get this code to execute even when the focused on the iframe?

<Script>
  function check(){ alert("test"); }
</script>

<body onkeydown="check()";>
  Onkeydown event only works when focus is not on iframe...<br>
<iframe src="about:blank" frameborder=1;> </iframe>
A: 

If the iframe is on a different domain than the hosting site you are out of luck. If not and you don't have control over it, you are out of luck. Else you could modify the iframe page to listen for the onkeydown event and invoke a parent javascript function:

window.parent.check();
Darin Dimitrov
would I be able to place the iframe inside a div / table and get the div / table to listen for the onkeydown event?
David
Not sure this would work but worth a try.
Darin Dimitrov
A: 

The iframe represents a different document and DOM events (e.g. key events) only bubble up within the same document. You will have to attach an additional key down listener to the iframe document.

Fabian Jakobs