Changing the page title with Jquery
How to make dynamic changing <title>
tag with jquery?
example: adding 3 >
symbols one by one
> title
>> title
>>> title
Solution 1:
$(document).prop('title', 'test');
This is simply a JQuery wrapper for:
document.title = 'test';
To add a > periodically you can do:
function changeTitle() {
var title = $(document).prop('title');
if (title.indexOf('>>>') == -1) {
setTimeout(changeTitle, 3000);
$(document).prop('title', '>'+title);
}
}
changeTitle();
Solution 2:
There's no need to use jQuery to change the title. Try:
document.title = "blarg";
See this question for more details.
To dynamically change on button click:
$(selectorForMyButton).click(function(){
document.title = "blarg";
});
To dynamically change in loop, try:
var counter = 0;
var titleTimerId = setInterval(function(){
document.title = document.title + '>';
counter++;
if(counter == 5){
clearInterval(titleTimerId);
}
}, 100);
To string the two together so that it dynamically changes on button click, in a loop:
var counter = 0;
$(selectorForMyButton).click(function(){
titleTimerId = setInterval(function(){
document.title = document.title + '>';
counter++;
if(counter == 5){
clearInterval(titleTimerId);
}
}, 100);
});
Solution 3:
using
$('title').html("new title");
Solution 4:
var isOldTitle = true;
var oldTitle = document.title;
var newTitle = "New Title";
var interval = null;
function changeTitle() {
document.title = isOldTitle ? oldTitle : newTitle;
isOldTitle = !isOldTitle;
}
interval = setInterval(changeTitle, 700);
$(window).focus(function () {
clearInterval(interval);
$("title").text(oldTitle);
});
Solution 5:
i use (and recommend):
$(document).attr("title", "Another Title");
and it works in IE as well this is an alias to
document.title = "Another Title";
Some will debate on wich is better, prop or attr, and since prop call DOM properties and attr Call HTML properties, i think this is actually better...
use this after the DOM Load
$(function(){
$(document).attr("title", "Another Title");
});
hope this helps.