Previous page on IronRouter

Is there a way to get the previous page layout before moving to the next page in IronRouter?

Is there any event that I can use to get this information?

Thanks in advance.

+5
source share
3 answers

You can achieve the desired behavior with hooks.

// onStop hook is executed whenever we LEAVE a route Router.onStop(function(){ // register the previous route location in a session variable Session.set("previousLocationPath",this.location.path); }); // onBeforeAction is executed before actually going to a new route Router.onBeforeAction(function(){ // fetch the previous route var previousLocationPath=Session.get("previousLocationPath"); // if we're coming from the home route, redirect to contact // this is silly, just an example if(previousLocationPath=="/"){ this.redirect("contact"); } // else continue to the regular route we were heading to this.next(); }); 

EDIT: used iron: router@1.0.0-pre1

+3
source

Since Iron Router uses the regular history API, you can simply use the simple JS method:

 history.go(-1); 

or

 history.back(); 

Edit: or check the previous path without following it:

 document.referrer; 

Strike>

+9
source

Apologies for running into the old thread, but it's good that this data was updated on the previous saimeunt answer, now deprecated, since this.location.path no longer exists in the Iron Router, so it should look something like this:

Router.onStop(function(){ Session.set("previousLocationPath",this.originalUrl || this.url); });

Or if you have a JSON session (see Session JSON )

Router.onStop(function(){ Session.setJSON("previousLocationPath",{originalUrl:this.originalUrl, params:{hash:this.params.hash, query:this.params.query}}); });

Only reservations with thisis that the first page will always fill url fields (this.url and this.originalUrl, there seems to be no difference between them) with the full url ( http: // ... ), While each the next page registers only the relative domain, i.e. / home without a root url, not sure if this is the intended behavior or not from IR, but this is currently a useful way to determine if this was the first page load or not

+1
source

Source: https://habr.com/ru/post/1202953/


All Articles