Change Tab Title with JavaScript

By  on  

Changing the tab (or window) title is an age old practice.  Gmail does it to notify the user of a new chat message and this blog does it to update the tab title after a new page loads via AJAX.  How is it done?  By setting a property on the document object, of course:

That property, of course, is document.title:

document.title = 'Hello!'; // New title :)

One common misconception is that you change the window.title property, but you must use the document object, otherwise you'll see no effect.  You'll oftentimes see a setInterval used with document.title to quickly change title to get the user's attention.

Recent Features

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Create Spinning, Fading Icons with CSS3 and MooTools

    A goal of my latest blog redesign was to practice what I preached a bit more;  add a bit more subtle flair.  One of the ways I accomplished that was by using CSS3 animations to change the display of my profile icons (RSS, GitHub, etc.)  I...

  • By
    Sexy Opacity Animation with MooTools or jQuery

    A big part of the sexiness that is Apple software is Apple's use of opacity. Like seemingly every other Apple user interface technique, it needs to be ported to the web (</fanboy>). I've put together an example of a sexy opacity animation technique...

Discussion

  1. This is great – in 15 years of working in web design the thought of changing page title never crossed my mind. Could be useful though, cheers!

  2. If you want to be evil about it change it to:

    document.title = '(3) mytitle'; 
    

    And see how many people wonder what is unread :)

    • If that’s not in evil.js, it should be added immediately

    • Matthew

      Thats pure evil.

  3. Loilo

    That’s really nice – I always did that via getElementsByTagName and innerHTML, but this solution’s way more convenient.

  4. Of-course this is tricky. It also affects DOM document ( element in HTML). Also In XUL, retrieving document.title before document gets completely loaded has unfixed behavior (document.title might pull an empty result or may effect-less.)

  5. I’m happy to be corrected on this, but if you change the tab title after a screen reader initially caches the page, they are not informed of this change. So, if you are changing the page for a relevant reason, could this be a problem? The page title is the first thing screen readers hear when a page loads, and it is like a road map to where they are, where they want to be.

  6. This is such a simple tweak and can make us look our website state of the art!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!