Forum Moderators: open
<script language="JavaScript">
function navigate()
{
// next
if(event.type == 'keydown' && event.keyCode == 39)
{
document.location.href="nextpage.html";
}
// prev
else if(event.type == 'keydown' && event.keyCode == 37)
{
document.location.href="prevpage.html";
}
// index
else if(event.type == 'keydown' && event.keyCode == 38)
{
document.location.href="index.html";
}
}
document.onkeydown = navigate;
</script>
Don't know what you mean by "cross-platform", but if that includes PDAs and smart phones then you should consider that these devices might not have keyboards and/or browser that supports version of JavaScript necessary to process these events (v1.3+?). Definately make sure you only execute this code on browsers that are capable of interpreting it correctly.
Only highly unusual scenarios really need direct key press/release handling - this is not a common way of navigating webpages, and it might be better idea to leave that to browser and common page markup.
If you want to make your code truly cross-platform then don't implement it in the first place.
Criticize away, I don't mind. So how would I (actually, you) rewrite it using keyup and have it work in IE/Mozilla/Safari...
Maybe my use of the term cross platform is incorrect. I just want it to work on most desktop browsers... IE, Mozilla and Safari would be good enough for me.
I basically want to be able to use arrow keys to go through pages.
And RE: Access keys, that requires a key combination, I want to use just the arrow key by itself to jump to a different page.
Thanks.
<html>
<body><script language="JavaScript1.2">
function navigate(Event)// Event appears to be passed by Mozilla
{
var moveto;// IE does not appear to pass it, so lets use global var
if(Event==null)
Event=event;if(Event.type!='keyup')
return;//alert("Code: "+Event.keyCode);
switch (Event.keyCode)
{
case 37: moveto="prevpage.html"; break;case 38: moveto="index.html"; break;
case 39: moveto="nextpage.html"; break;
default: return;
}document.location.href=moveto;
}document.onkeyup=navigate;
</script>text text text
</body>
</html>
Testing with Opera and other marginal browsers is left as an exercise to interested parties. I will send you my invoice later :)
And RE: Access keys, that requires a key combination, I want to use just the arrow key by itself to jump to a different page.
I expect I'm not the only person who uses the arrow keys to scroll up and down webpages. If they suddenly started navigating between pages I'd be slightly confused. It is not what I expect.