Change Title With Javascript
Asked Answered
P

4

24

How can I modify the existing title to the document with Jquery as mouseover title change like that on facebook title link.

Parkins answered 16/7, 2011 at 4:32 Comment(3)
You can see an example jQuery code here.Dragnet
possible duplicate of Changing the page title with JqueryGagliano
Possible duplicate of How to dynamically change a web page's title?Burrus
D
40

You don't need jQuery.

document.title = 'My new title here';
Dextrad answered 16/7, 2011 at 4:34 Comment(0)
M
11

With javascript. jQuery won't help you here:

document.title = 'New Title';

You can insert that into a jQuery mouseover callback function if you want.

Molder answered 16/7, 2011 at 4:34 Comment(0)
C
7

I'll extend on these other answers, this code should do it in entirety, just be sure to change the class in the selector, and the new Title Text.

(function(){
    var oldtitle;
    jQuery('a.yourlink').hover(
        function () {
           oldtitle = document.title;
           document.title = 'Your New Title';
        },
        function () {
            document.title = oldtitle;
        }
    );
})();

Here is a jsfiddle demo I made that changes the text of the object, rather than the window title: http://jsfiddle.net/MpZGf/1/

Cultivation answered 16/7, 2011 at 4:40 Comment(3)
It looks like oldTitle will be out of scope when it's needed the second time.Dissociation
This won't work. oldtitle won't exist when the 2nd function is called. You have to move oldtitle up to a higher scope or save it somewhere else.Monetmoneta
wouldn't it be better to wrap the lot in a function and not have to attach extra 'properties' to the document like here - jsfiddle.net/aranm/gUsnC/2Frostwork
I
4

Try:

document.title = 'title';
Illative answered 16/7, 2011 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.