Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change content of title tag, when switching to other open tab in the browser

I just recently saw this on two different websites, does anyone know how it is done? If you have multiple tabs open, and you leave the current one, the title in the tab is changed. Very nice trick!

http://blog.invisionapp.com/

http://zerosixthree.se/create-a-responsive-header-video-with-graceful-degradation/

like image 254
fluxus Avatar asked May 26 '14 22:05

fluxus


People also ask

What is the <title> tag in HTML?

The <title> tag defines the title of the document. The title must be text-only, and it is shown in the browser's title bar or in the page's tab. The <title> tag is required in HTML documents! The contents of a page title is very important for search engine optimization (SEO)!

Which element defines a title in the browser toolbar?

The <title> element: defines a title in the browser toolbar. provides a title for the page when it is added to favorites. displays a title for the page in search-engine results.

How to optimize your page title with keywords?

This is a great way to optimize your title with keywords. The page title format applies to all pages except your homepage and collection items. By default, the page title format is Page Title — Site Name, using the variables %p and %s. %p pulls in the SEO title for that page.

How do I add a page SEO title to a link?

To have the title of the page, product, or blog post be the first words in a browser tab or search results link, place the page SEO title ( %p) or item SEO title ( %i) at the beginning of the title format. This places more importance on the content of the page rather than the entire site.


2 Answers

This works by registering Handlers on the onfocus and onblur events of window.

jQuery-Style:

$(window).on('blur', function() { ... });

Without jQuery:

window.onblur = function() { ... }

If that was not clear: the pages title can be read/written via document.title

like image 189
marstato Avatar answered Oct 24 '22 08:10

marstato


$(function() {
  var message = "Don't forget us";
  var original;

  $(window).focus(function() {
    if (original) {
      document.title = original;
    }
  }).blur(function() {
    var title = $('title').text();
    if (title != message) {
      original = title;
    }
    document.title = message;
  });
});
like image 40
Yogesh Chauhan Avatar answered Oct 24 '22 07:10

Yogesh Chauhan