Search code examples
javascriptdom-eventsecmascript-5

Trouble using array.some and general code hash up. Navigation function to correct div/section


I've made a general hash up of the following function. Basically linking this function to the onclick method of a button. The idea being that if the next page/div is visible to navigate there else navigate to the next one, and so on. If there are no further pages visible (from the current page) then alert the user. Just in case they carry on clicking.

Here is my code:

function showNext(id){

var currPage = id.match(/\d+/)-1;
var pages = [document.getElementById("page2"),document.getElementById("page3"),document.getElementById("page4")];
var next = ["page2marker","page3marker","page4marker"];
var valid = false;

for (var i=currPage; i<=pages.length; i++){
    var Icansee =  pages.some(function() { pages[i].style.display == "block"});
    if(Icansee){
        valid = true
    }
    if(valid){
        return window.location.hash = next[i];
    }
    if(!valid){
        alert("No other pages to navigate to");
    }
}
}

I know that my use of the array some function is incorrect, along with plenty of other things. Just need a general shove in the right direction.

Edit: Just realised that array some is an ECMAScript 5 addition which isn't supported by the piece of software that I'm using. So I will need to find another way of solving this one. Any ideas?


Solution

  • There's a sample implementation of Array.prototype.some for browsers that don't support it natively at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some