0

What approach should for this scenario. I want the page to go to the next page after it finishes all the function it needs to.

so, example. after all function for page1.html has been done, it will call a function next_page().

next_page() function will evaluate the current page and add "1" it. so from page2.html it will now be page3.html. page3.html will also contain the same function of the previous html, that after all the functions have been done, it will call the next_page() function that will also evaluate the current and increment it.

//current_url = "mysite.com/page1.html"
var current_url = window.location;
var end_page = "mysite.com/page12.html"


var increment_url = eval(current_ur + 1 );

    if(current_url != end_page ) {
        setTimeout(next_page,2000)
    }
    else {
        alert("this is the last page!")                    
    }


function next_page() {
    window.location.replace(increment_url);       
}
Pennf0lio
  • 3,888
  • 8
  • 46
  • 70
  • Do you know window.location.replace prevents the user from going back to the previous page? Just checking you haven't used it by accident instead of window.location – Popnoodles Nov 24 '12 at 19:31
  • @popnoodles I wasn't aware I can't go back to the previous page using window.location, but not it's really big deal. But, incase what's an alternative methed to do similar function? – Pennf0lio Nov 24 '12 at 20:38
  • no you can with window.location, you can't with window.location.replace http://stackoverflow.com/questions/1865837/whats-the-difference-between-window-location-and-window-location-replace – Popnoodles Nov 24 '12 at 22:26

2 Answers2

3
var increment_url = addone('mysite.com/page1.html');

returns mysite.com/page2.htm

function addone(url) {
    var pattern=new RegExp("(.*)([0-9+])(\..*)");
    var match=pattern.exec(url);
    return match[1] + (parseInt(match[2]) + 1 ) + match[3];
}
​

so assuming the example URL you gave is accurate enough that the regular expression will work use

var increment_url = addone(current_url);
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
0

The easiest thing to do would be to have a hidden input on each page that has the value of the next page url. This would allow you to use arbitrary page urls and still be able to get the effect you want.

Using jQuery

 $(function() {
     setTimeout(function() {
          var url = $('#next_page_url').val();
          window.location.replace(url);
     }, 2000);
 });

 <input type="hidden" id="next_page_url" value="http://mysite.com/page2.html" />
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • I would change the input type for an a tag, it describes better that it contains a link. – jaudette Nov 24 '12 at 19:32
  • It's not a link, it's a hidden field – Popnoodles Nov 24 '12 at 19:33
  • But in any case the OP is asking for an arbitrary function, a slight rewrite of what they put, to get the next page from the current URL, not output a hidden field. – Popnoodles Nov 24 '12 at 19:34
  • @popnoodles In the immortal words of Mick Jagger, "you can't always get what you want, but if you try, sometimes, you just might find, you get what you need." – tvanfosson Nov 24 '12 at 19:48
  • not sure why this is `easiest` when it requires html modifications to numerous pages, whereas parsing url only requires script – charlietfl Nov 24 '12 at 19:55
  • @Tvanfosson, this can work too but this is not the approach i'm looking as i would be placing hidden fields on every page of the site. thanks though :) – Pennf0lio Nov 24 '12 at 20:34
  • @charlietfl - easiest from a code perspective. easiest from the perspective that it's completely independent of the page name (consider if you want to insert a page in a long sequence). The analogy I would use is a linked list vs an array approach, though it would be relatively easy to support branching (conditional paths) with this method as well. – tvanfosson Nov 24 '12 at 21:07
  • @tvanfosson I see your thinking about branching, conditionals etc. Different perspectives potentially for backend vs front end developer too. – charlietfl Nov 24 '12 at 21:18
  • @tvanfosson Jagger, in that quote, talked about trying. By trying to give what the person asking the question actually wanted, they got it, as opposed to what he didn't need. – Popnoodles Nov 24 '12 at 22:35