iron:router check if route loaded with history.back

362 views Asked by At

I try to detect how the user got to the route. There are 3 cases I guess:

  1. The page was loaded directly by the URL
  2. The page was loaded by clicking on a Link
  3. The page was loaded by clicking on the history back or forward buttons in the browser

Is there any way to detect the 3 cases or - which would be enough for me - to detect if the user came to the route by the 2nd case ?

Why ? In my project I have a List-Layout. When I am scrolling down and clicking on a Link in this List the new Template will be rendered but the window has now a scrollTop Value because of the scrolling in the list.

I fixed this with this:

Router.configure({
  onAfterAction : function () {
    jQuery(window).scrollTop(0);
  }
});

With this fix after every page load the page will be scrolled to the top (like its normal).

But:

When I clicked on a link in the list and then I click on the "Back" button in my Browser (history.back) the page will also be scrolled to the top (because of my fix) - without the fix the page will be automatically jump to the last position.

Because of this I need something like this:

Router.configure({
  onAfterAction : function () {
    var historyBack;
    historyBack = CheckIfHistoryBackWasClicked();
    if (!historyBack) jQuery(window).scrollTop(0);
  }
});

Anyone any idea ?

1

There are 1 answers

4
Thiago Duarte On

Try to add a click event handler to your links and set a scrollToTop session variable there:

"click a": function(e) {
    Session.set("scrollToTop", true);
    return true;
}

Then, do something like this:

Router.configure({
  onAfterAction : function () {
    if (Session.get("scrollToTop") == true) {
      jQuery(window).scrollTop(0);
      Session.set("scrollToTop", false);
    }
  }
});