Skip to content Skip to sidebar Skip to footer

How To Change Page With Javascript For Keyboard Navigation?

I would like to allow keyboard navigation for a photo gallery website (one page, one photo). What's the Javascript function to do this? I use the code below to manage keyboard even

Solution 1:

You need a document.location:

functioncheckKeycode(e)
{
  var keycode;

  if (window.event)
    keycode = window.event.keyCode;
  elseif (e) keycode = e.which;

  switch (keycode)
  {
    case37:  // left arrowdocument.location = "page1.htm";
    break;

    case39:  // right arrowdocument.location = "page3.htm";
    break;
  }
}

Solution 2:

As long as your pages are neatly named, you could use

document.location = "http://www.myURL"

But you'll need a cycle for your pages. There are many ways of doing this, such as appending a number to a string that you pass:

var count = 0;
var html;
functionprevious(){
   html = "http://www.page" + count + ".htm"document.location = html;
}
functionnext(){
   var count +=1;
   html = "http://www.page" + count + ".htm"document.location = html;
}

or storing the strings in an array etc.

Solution 3:

[2022 Updated Version]

document.location is the function I needed:

document.onkeydown = checkKey;

functioncheckKey(e)
{
  e = e || window.event;

  switch (e.key)
  {
    case"ArrowLeft":
      document.location = "page1.htm";
    break;

    case"ArrowRight":
      document.location = "page3.htm";
    break;
  }
}

Post a Comment for "How To Change Page With Javascript For Keyboard Navigation?"