PDA

View Full Version : Help with KeyPress



TimFA
03-14-2007, 02:40 AM
I want to have the user redirected to another page when any key is pressed. I know this is fairly simple to do, but I cannot seem to find anything to do it.

Thanks,
Tim

tech_support
03-14-2007, 08:47 AM
<script type="text/javascript">
document.onKeyPress = function() { window.location = "anotherpage.html" }
</script>

Twey
03-14-2007, 08:54 AM
<script type="text/javascript">
document.onKeyPress = function() { window.location = "anotherpage.html" }
</script>It's onkeypress, not onKeyPress: Javascript is case-sensitive. Also, using window.location like a string is a bit of a hack, and has inconsistent behaviour with other types of window (like frames). You want to provide something for non-JS browsers, too.
<script type="text/javascript">
document.onkeypress = function() {
location.href = "newpage.html";
};
</script>
<noscript>
<p>
Please click <a href="newpage.html">here</a> to continue.
</p>
</noscript>

TimFA
03-14-2007, 03:12 PM
Thank you for your help.

Tim