Changing the page title with Jquery
Asked Answered
S

10

94

How to make dynamic changing <title> tag with jquery?

example: adding 3 > symbols one by one

> title
>> title
>>> title
Senecal answered 24/8, 2011 at 10:2 Comment(0)
R
207
$(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();
Readiness answered 24/8, 2011 at 10:5 Comment(2)
Might it not be better to use setInterval and clearInterval rather than a string of setTimeouts?Irredentist
What are the implications on SEO calling the page via jQuery?Tile
I
23

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);
});
Irredentist answered 24/8, 2011 at 10:4 Comment(1)
when user click on button, title will be changed like in question, and this changing will be loopedSenecal
A
5

using

$('title').html("new title");
Accompanist answered 24/8, 2011 at 10:4 Comment(3)
i know, but how make it dynamically?Senecal
well this IS dynamic, ie with Javascript. Would you by chance mean "animated"?Aventurine
@IIya: give more markup and code for better answer . your questions unclear.Accompanist
A
3

 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);
        });
Argali answered 20/1, 2016 at 11:51 Comment(0)
C
3

Html code:

Change Title:
<input type="text" id="changeTitle" placeholder="Enter title tag">
<button id="changeTitle1">Click!</button>

Jquery code:

$(document).ready(function(){   
    $("#changeTitle1").click(function() {
        $(document).prop('title',$("#changeTitle").val()); 
    });
});
Cosgrove answered 17/3, 2018 at 8:50 Comment(0)
U
2

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.

Uncommunicative answered 18/10, 2016 at 1:3 Comment(0)
D
1

Some code to walk through a list of titles (circularily or one-shot):

    var titles = [
            " title",
            "> title",
            ">> title",
            ">>> title"
    ];

    // option 1:
    function titleAniCircular(i) {
            // from first to last title and back again, forever
            i = (!i) ? 0 : (i*1+1) % titles.length;
            $('title').html(titles[i]);
            setTimeout(titleAniCircular, 1000, [i]);
    };

    // option 2:
    function titleAniSequence(i) {
            // from first to last title and stop
            i = (!i) ? 0 : (i*1+1);
            $('title').html(titles[i]);
            if (i<titles.length-1) setTimeout(titleAniSequence, 1000, [i]);
    };

    // then call them when you like.
    // e.g. to call one on document load, uncomment one of the rows below:

    //$(document).load( titleAniCircular() );
    //$(document).load( titleAniSequence() );
Deil answered 24/8, 2011 at 11:2 Comment(0)
A
1

I use this one:

document.title = "your_customize_title";
Alvertaalves answered 22/6, 2015 at 13:29 Comment(0)
C
1

Its very simple way to change the page title with jquery..

<a href="#" id="changeTitle">Click!</a>

Here the Jquery method:

$(document).ready(function(){   
    $("#changeTitle").click(function() {
       $(document).prop('title','I am New One');
    });
});
Cosgrove answered 17/3, 2018 at 5:8 Comment(0)
B
0
document.title="your title";

I prefer this.

Bible answered 5/9, 2015 at 18:35 Comment(1)
This is just a repeat of the existing answers.Menorrhagia

© 2022 - 2024 — McMap. All rights reserved.