Help with Iron Router


I’m trying to figure out how i can use iron router (or something else?) to get this:

when a user goes to

i want this to happen

Session.set('currentPage', 'XXXX');

Is this possible?


1 Like

I would use onBeforeAction: or onAfterAction:.


Router.route('/XXXX', {
    name: 'someTemplateNme',
    onBeforeAction: function () {
       Session.set('currentPage', 'XXXX');

Hmm let me rephrase my problem.

If the user types
i want to set Session.set(‘currentPage’, ‘XXXX’);
to whatever the user typed in the adressbar (after the slash)

So if the user types
I want Session.set(‘currentPage’, ‘randomword123’);

Modifying chompomonim’s response:

Router.route('/:someValue', {
    name: 'someTemplateNme',
    onBeforeAction: function () {
       Session.set('currentPage', this.params.someValue);

Using a colon : in the path tells the router to expect a variable value there, which can then be accessed as a key in the this.params object, with the value that was in place of that key in the path.

Have a look at the iron router docs for more details:

Thank you! It works nicely

How can i change without refreshing the page?

Inside this one{
    'submit form': function(event){
        var board =;
        Session.set('currentPage', board);
        document.title = board;
        /*please change the address bar to board) without refreshing the page)*/

I found a solution with pure javascript

window.history.pushState('Object', 'Title', variable);


Again I’m going to point you back to the iron router docs, they’re very helpful. I very much recommend taking a look through if you find yourself using iron router a lot.

Iron Router Navigation

But more simply:{
    'submit form': function(event){
        //your other code

If you’re already using iron router (heck, Meteor) I wouldn’t recommend this:

I’m using your solution now, less code, looks better, works as well :smile:

Thank you for your help, much appreciated.

Sorry for spamming, but a quick question.

Would this be the correct way to do it using a variable? (It works)


(Couldn’t find anything about variables in the docs, sorry)

Yes, as far as I can tell. The first parameter can be either a path (as above) or the name of the route, if you supplied it where you defined your routes. I believe either way it has to be a string.

And don’t worry, asking questions isn’t spamming.

Isn’t better to just call Router.current().url whenever you want in the client?

Router.route(':page', {
  name: 'home',
  onBeforeAction: function() {

To go to another page use

Router.go('home', {page: 'users'});

This will make you go to /users and make Session.get('currentPage'); return users