how can i prevent a html page from scrolling when arrow keys are pressed if a iframe inside it is focused?
im gettting this error in chrome
The iframe is focused, i know its focused. the parent scrolls anyway.
how can i prevent a html page from scrolling when arrow keys are pressed if a iframe inside it is focused?
im gettting this error in chrome
The iframe is focused, i know its focused. the parent scrolls anyway.
The following code inside the iframe document will prevent it from scrolling:
document.onkeydown = function(evt) {
evt = evt || window.event;
var keyCode = evt.keyCode;
if (keyCode >= 37 && keyCode <= 40) {
return false;
}
};
This works except IE:
window.top.document.onkeydown = function(evt) {
evt = evt || window.event;
var keyCode = evt.keyCode;
if (keyCode >= 37 && keyCode <= 40) {
return false;
}
};
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.
I have the same issue both frames scroll with the arrow keys when focused on the iframe