Change Page Title on Focus Out Using jQuery
It is a good idea to change the page title to something catchy when the user moves to a different tab to catch his attention. This can help to get the user to come back your website. So in this post, let’s find out how to change the title of the page when user moves to different tab using jQuery.
Here is the jQuery code which will change the page title to “something of your choice” on focus out and set the original title when the focus is back on the page.
var oldTitle = $(document).find("title").text();
var newTitle = "Read this...";
document.title = title;
The above code does following:
- First, it stores the original title of the page in a variable.
- Then, it initializes another variable “newTitle” to store the new title. For the demo, it is set to “Read this…” but you are free to change.
- Defines a setTitle function which set the title of the page.
- And then attaches focus and a blur event to the window to change the title. When focus moves out, then sets the new title. And when focus is back on the window, sets the old stored title.