Title of URL in Browser tab title w/ Iron Router

I’m simply trying to change the Browser tab title to the URL, when the URL changes.

For example if I’m on

greatapp.com/messages

the Browser tab title would be messages, and if I’m on

greatapp.com/alerts

the Browser tab title would be alerts.

I’ve seen here where an onAfterAction is used to change document.title, but apparently there are issues with this approach.

I’m wondering what others have done to solve this issue or is onAfterAction this best way forward.

Hey @aadams
this is the approach we use:

Router.map(function() {
  return this.route('imprint', {
    path: '/imprint',
    onAfterAction: function() {
      return setTitle('Imprint');
    }
  });
});

this.setTitle = function(title) {
  var base;
  base = 'My Meteor App';
  if (title) {
    return document.title = title + ' - ' + base;
  } else {
    return base;
  }
};

I don’t know about other solutions but this approach has always served us well.

2 Likes

I really like this package for setting titles.

https://atmospherejs.com/yasinuslu/blaze-meta

1 Like

This package works great – thanks for the suggestion!

1 Like