Forum Moderators: open

Message Too Old, No Replies

How do I make this simple key press code cross platform compatible?

Only works in IE, how do I make it work on Mozilla/Safari?

         

FG_Man

4:52 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



Basically I want to implement using the arrow keys to navigate my pages. Right arrow to go to the next page, left to go back, up to go index. I got the following code working on IE, but not on any other browsers. How do I make it cross-platform? Thanks.

<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>

Lord Majestic

5:00 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not trying to critisise, but it might make more sense to act on "key up", just like mouse click is acted upon release of pressed mouse button.

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.

drbrain

5:05 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't use JS, use accesskey [w3.org].

FG_Man

6:25 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



>Not trying to critisise, but it might make more sense to act on "key up"

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.

Lord Majestic

7:50 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I modified your code a bit and it now appears to work both in IE6 and current Firefox v0.9.3. The main difference between the two appears to be linked to the fact that IE has got global "event" var, where as Mozilla passes it as argument to function.


<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 :)

py9jmas

7:57 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



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.

FG_Man

8:56 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



That's a good point re: remapping the arrow keys. I didn't think of that......

Maybe I'll only remap the left and right arrows... I know I personally hardly ever use those to go left and right on pages. Usually I just resize window to fit width wise....