Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- JavaScript location = bookmark/id on same page?


Dabrowski - 9:37 pm on Apr 27, 2007 (gmt 0)


It's worth noting that for that one, I assigned onkeydown to document, instead of keypress. That will work better, as you just want a single key press it will act when the key is pressed, rather than when it's released. Won't work for shifted or ctrl, etc..... have to change it back to keypress if you want those.

ok, got your head around that one? Now lets chop that code down to size..........

First we're going to stick that growing number of IDs in an array, which corresponds to their keycode:

var ids = new Array();

ids[ 49] = "#ha";
ids[ 50] = "#hb";
ids[ 51] = "#hc";
ids[ 52] = "#hd";
ids[ 53] = "#he";
ids[ 54] = "#hf";
ids[ 55] = "#hg";
ids[ 56] = "#hh";
ids[ 57] = "#hi";
ids[ 58] = "#hj";
ids[ 99] = "#content";

Don't worry about the gaps in the numbers, they'll automatically get filled in with undefined's. Since not all these bits of code are the same, you still need a little switch, but the rest is identical.

Now, with Drag_Racer's location.hash method we can get rid of 2 pages of code by doing this:

if( ids[ key]) {
menuhide();
location.hash = ids[ key]; // I know he'll correct this to window....
}
else switch( key) {
......
your focus events and escape key code
......


Thread source:: http://www.webmasterworld.com/javascript/3320399.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com