0

I have a:

var pageURL = location.href; // stores the URL of the current page in the var pageURL

And I have :

var Page = Array ();
page [0] = "http://web1.com"
page [1] = "http://web2.com"
page [2] = "http://web3.com"

Now I want to make a function (called nextPage) to check which one of these pages in the array equals the PageURL. Also I need to have a button, so when I click it it will take me to the next page. What that mean I want to increment the page by 1.

Maansahir
  • 29
  • 1
  • 4
  • 8

3 Answers3

1

You can use very simply

var current = page.indexOf(location.href);

Then to go to the next page

location.href = page[current + 1];
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • @AtesGoral mostly it is. It actually has good support from most browsers with the exception of IE. Even then, though, it is now [supported in IE9](http://kangax.github.com/es5-compat-table/). – Joseph Marikle Jan 11 '12 at 00:01
  • @JosephMarikle I hear you. But unfortunately "mostly" doesn't cut it in the wild. You're either cross-browser or you're not. – Ates Goral Jan 11 '12 at 00:08
  • You might condsider: `page[(current + 1) % page.length]` so that it doesn't attempt to navigate to *undefined* for the last page and wraps to the first page instead. – RobG Jan 11 '12 at 00:08
  • @JosephMarikle The right thing to do is to use `indexOf` in the presence of a polyfill, until a reasonable percentage of browsers start supporting it. You cannot rule out < IE9 as of yet. – Ates Goral Jan 11 '12 at 00:10
  • @AtesGoral I Understand... We can't even rule out IE6 as of yet for goodness sake. D: But yes, thank you for pointing it out. – Joseph Marikle Jan 11 '12 at 00:43
  • I just tried this example in IE 9, and browser mode 8 and 7 and indexOf worked in all of them. Is it IE 6 that everyone is concerned that it won't work in? – Luis Perez Jan 11 '12 at 02:30
  • The other modes for IE 9 are mostly for rendering, I don't think it changes the script engine. In IE 8, `Array.prototype.indexOf` is `undefined`. IE 6 to 8 have about 3 times the use of IE 9 if you believe [browser statistics](http://w3schools.com/browsers/browsers_explorer.asp). – RobG Jan 11 '12 at 02:43
0
    var form = document.createElement('form');
    form.id = "nextForm";
    document.body.appendChild(form);

    var nextButton = document.createElement('input');
    nextButton.type = 'button';
    nextButton.id = 'nextButton';
    nextButton.value = 'Next Button';
    nextButton.onclick = nextPage;
    form.appendChild(nextButton);

    function nextPage() {



        var page = Array ();
        page[0] = "http://web1.com" // insert your urls here
        page[1] = "http://web2.com"
        page[2] = "http://web3.com"


        var matchIndex = page.indexOf(location.href);
        var nextIndex;
        if (matchIndex !== -1 && page[matchIndex + 1]) {
            nextIndex = matchIndex + 1;
        } else {
            alert("You're at the last page"); // next page in the array does not exist, you can handle the error however you want
            return;
        }

        goToNextPage(page[nextIndex]);
    }
    function goToNextPage(url) {
        document.location.href=url;
    }
0

Based on Joseph's answer the full code would look something like this:

function goToNextPage() {
    var currentPageUrl = location.href;

    var pageUrls = [
        "http://web1.com",
        "http://web2.com",
        "http://web3.com"
        ];

    var currentIndex = pageUrls.indexOf(location.href);
    var nextIndex = currentIndex + 1;
    var nextPageUrl = pageUrls[nextIndex];
    location.href = nextPageUrl;
}

Also if support for indexOf is an issue for you in IE check out the answer to that from another StackOverflow question: How to fix Array indexOf() in JavaScript for Internet Explorer browsers

Community
  • 1
  • 1
Luis Perez
  • 27,650
  • 10
  • 79
  • 80