I am trying to make 开发者_JS百科a simple shortcut feature for my site and I am very new in JavaScript. Basically simple thing: when somebody presses n-key (keycode:78) jump to page "display.php", and I want this to work on all platforms (IE, Mozilla etc.).
Use this:
<script type="text/javascript">
document.onkeyup = KeyCheck;
function KeyCheck(e)
{
var KeyID = (window.event) ? event.keyCode : e.keyCode;
switch(KeyID)
{
case KEY_CODE:
document.Form1.KeyName.value = "...";
<!-- DO STUFF -->
}
}
</script>
To ensure cross-browser functionality it is a good idea to rely on common libraries like jQuery. There's a method to do this: http://api.jquery.com/keypress/
Try this
$('body').keypress(function(event) {
if (event.which == '78') {
window.location.href = 'yourURL';
}
});
精彩评论