Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, seems to me you'll need to know which page you're currently on each time for it to work. For this I'd recommend window.localStorage if (and only if) A) all of the pages are served from the same domain, and B) You don't need to support older browsers. If either of those are untrue, this method won't work, and you'll need to do something else like parse the URL string.</p> <p>I took your code and modified it slightly to show how you might use localStorage. I added some comments, but it should be relatively self-explanatory. Here 'tis:</p> <pre><code>//if current index don't exist, make it if (!window.localStorage.currentPageIndex) { window.localStorage.currentPageIndex = 0;//frustratingly, it gets stringified anyway - you have to parseInt later } //set up vars var pages = ["index.php", "random-page.php", "some-page.php", "another-page.php", "and-so-on.php"], currentPageIndex = parseInt(window.localStorage.currentPageIndex);//as promised //set event listener on window window.addEventListener("keyup", KeyCheck); function KeyCheck(e) { var KeyID = (window.event) ? event.keyCode : e.keyCode; switch(KeyID) { // left arrow key case 37: if (currentPageIndex == 0)//we're at the first page, go to the last currentPageIndex = pages.length - 1;//-1 to account for first index being "0" else//go to the previous page currentPageIndex--; break; // right arrow key case 39: if (currentPageIndex == (pages.length - 1))//if we're at the last page, go to the first currentPageIndex = 0; else//go to the next page currentPageIndex++; break; default: var noChange = true;//just so we can ignore the rest if a boring key } if (!noChange)//haha, I love double negatives { //now dump the new current page index back into localStorage window.localStorage.currentPageIndex = currentPageIndex; //first, try it in your javascript console to make sure it works (refresh the page!) console.log(pages[currentPageIndex],currentPageIndex); //then use real urls and uncomment the next line to go to the new current page! //location.href = pages[currentPageIndex] } } </code></pre> <p>But - I've gotta ask - do you really want to do it this way? that's a lot of HTTP requests and refreshing the page - are the pages small enough that you could just load them all together at once, and simply only display one at a time? (you could even do a cool sliding or crazy 3d effect between pages - again, assuming you only need to support newer browsers...)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload