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.

2
  • 1
    Looping through an array is a fairly basic programming task. I might recommend reading up on some tutorials Commented Jan 10, 2012 at 23:55
  • Or use this polyfill to use the ES5 Array.prototype.indexOf: gist.github.com/1034425 Commented Jan 10, 2012 at 23:57

3 Answers 3

1

You can use very simply

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

Then to go to the next page

location.href = page[current + 1];
Sign up to request clarification or add additional context in comments.

7 Comments

@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.
@JosephMarikle I hear you. But unfortunately "mostly" doesn't cut it in the wild. You're either cross-browser or you're not.
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.
@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.
@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.
|
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;
    }

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.