Alex Rivera | Logout

Prevent scrolling with arrow keys

Asked 2010-07-08T21:07:36.457
9

How can I prevent an HTML page from scrolling when arrow keys are pressed if an iframe inside it is focused?

I'm gettting this error in Chrome

The iframe is focused, I know its focused. The parent scrolls anyway.

Edit
Report

1 Answer

1

This code does the trick:

JavaScript

<script type="text/javascript">
  function focusOnIframe(iFrameID) {
    if (frames[iFrameID]!=undefined)
      frames[iFrameID].focus(); // Works in all browser, except Firefox
    else
      document.getElementById(iFrameID).focus();  // Works in Firefox
  }
</script>

HTML (example)

<input type="button" id="setfocus" value="Set focus" onclick="focusOnIframe('myiframe')" />

<p>Bla<br />Bla<br />Bla<br />Bla<br />Bla<br /></p>  <!-- Just some filler -->

<iframe id="myiframe" src="yourpage.html"></iframe>

<p>Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br /></p>  <!-- Just some filler -->

I've tested it in Firefox 3.6.6, Iron 5.0.380, Opera 10.60, IE 6 and IE 8.

answered 2010-07-14T02:49:37.153

Your Answer