Search code examples
javascriptmeteoriron-router

How to jump 2 pages before using iron router?


I want to jump to the previous page using iron router. Normally, I would have used the history.back() function but in this case, I use yield templates.

The page I am in is reached using myWebsite.com/page which redirect to myWebsite.com/page/sub-page in order to display the default yield content. Tell me if it is not clear enough, I can edit and provide the code.

To sum it up, my previous page is 2 pages behind. I tried history.back(2) or history.back(-2)but it does not change anything to the normal behavior of the function. How should I do?

I could point directly to the second page but if the user uses the myWebsite.com/page url to access it, my back button would not work.

EDIT: It appears that it was my condition for checking if the history was at least 2 pages which didn't work. Moreover, I should use history.go(-2) instead of history.back()

I will post a proper answer.


Solution

  • Here is the code:

    var numberOfEntries = window.history.length;
    //check if the history is big enough
    if (numberOfEntries>2){
         // if it is, jump two pages before
        history.go(-2);
        }else {
        //if it is not, use the default path
        Router.go("home");
        }