Question

I am using knockout and sammy.js for my small single page application.

While navigating during the routing, I need to modify title of my browser window. Sammy.js has a specific plugin for dealing with this, but looking at the source code it looks to me that this is just a simple wrapper around document.title = '...';.

So what advantage does this plugin give over plain document.title inside of my route?

Was it helpful?

Solution

The only thing needed to change the title of the window is modifying the document.title attribute. What this Sammy plugin offers is a way to build titles.

Looking at their source code, they offer 2 options:

(1) Use setTitle and give it a String. This will set the first part of the title that will continue to be used throughout the app. It will not change the browser's title.

Example (from their source code):

this.setTitle('My App -');

Now what you would do next for each and every route where you want a unique title is this:

this.title('Home');

The resulting title that will now appear in the browser's title is: "My App - Home"
On another page it could be: "My App - Services"
and so on. This allows you to easily keep a static part to the title in case you never want it to go away.

(2) Provide your own function which builds the title. This is useful if you have a lot of conditions or special rules to follow. Your function could simply convert everything to uppercase or whatever else you need.

Example:

this.setTitle(function(title) {
    return title.toUpperCase();
});

Now when you set the title it will always be uppercase.

this.title('Home');

The resulting title will be: HOME

So short answer, if you have simple titles you want to use then by all means use document.title directly. If you have rules, conversions or want to keep the company name at the beginning to matter the page title then you may want to use the plugin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top