Javascript Keyboard Navigation

/ Published in: JavaScript
Save to your folder(s)

Starting with GoogleTV in mind I made a script to help navigate a page key basic key inputs, in this case I'm using the left and right arrows to jump between the H1 tags on a page, although the modifications to make it do other things would be very simple. Theres a more working concept on the link so you can see it in action without any coding on your part (and participate in some proper reading).

There is one downside to this method, if manually scrolling down past an H1 tag it does not realize it, but I'll work on that once I get a chance

Copy this code and paste it in your HTML
  1. function keycatch(){
  2. var keypress = event.keyCode
  3. var h1tags = document.body.getElementsByTagName("h1");
  4. switch(keypress){
  5. case 37: // Left
  6. if(currentpos!=0){
  7. currentpos = currentpos-1;
  8. h1tags[currentpos].scrollIntoView();
  9. }
  10. return false
  11. break;
  12. case 39: // Right
  13. if(h1tags[currentpos+1]!=undefined){
  14. currentpos = currentpos+1;
  15. h1tags[currentpos].scrollIntoView();
  16. }
  17. return false
  18. break;
  19. }
  20. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.