sureshunivers
sureshunivers

Reputation: 1753

Keypress event to web page not working from iframe

I created a web page using html and javascript. Used iframe inside the page and assigned keypress event to the window like,

<html>
  <head>
    <title>Keypress Event test</title>
    <script type="text/javascript">
      function onKeyPress(e){alert(e.keyCode);}
      window.addEventListener("keypress", onKeyPress);
    </script>
  </head>
  <body>
    <h1>Helow! Welcome!</h1>
    <iframe src="..." />
  </body>
</html>

On key press the alert is working fine. after click iframe and press key the function is not working. How can bring keypress event from iframe?

Upvotes: 1

Views: 969

Answers (1)

Prabu
Prabu

Reputation: 4187

If you place an iFrames on a page, then the browser will create a window object for the page, and one for each iFrame.

I suspect this is the cause of this problem, since your code does not explicitly say which window object to attach the keypress event to.

Upvotes: 1

Related Questions