tags:

views:

101

answers:

8

how to change the title of page through jquery?

+2  A: 

In pure JavaScript:

document.title = "Insert title here";

the document should be fully loaded before you change it.

Reference: Document.Title at Mozilla Developer Central

Pekka
It appears that caveat only applies to XUL, at least as far as that document mentions.
Carson Myers
@Carson yeah. I meant generally - no idea what happens if you change it in the `head` section. But you won't need to do that anyway.
Pekka
+2  A: 
$('title').html('newTitle')
sTodorov
A: 

Like any other page element:

 $("title").html("your new title");
Carson Myers
+3  A: 

Why jQuery for such minor task? Use vanilla javascript:

document.title = "My new title";

More Info:

If you still want to go with jQuery, you simply do:

$("title").html("My new title");
Sarfraz
+1  A: 

Simple

$("title").html("newtitle");

FOR IE

document.title = 'new title';

To change the favicon use something like this

HTML

<link REL="SHORTCUT ICON" HREF="http://yoursitedotcom.here/yourdir/favicon.ico" id="myicon">

Script

$("#myicon").attr("HREF","mynewicon.gif");
Starx
Ahem, typo, ahem... :)
Yi Jiang
this fails for me on IE8
Ivo van der Wijk
@Yi Jiang, Fixed.... @Ivo Vad Der Wijk, what does not work, the title or Favicon
Starx
Sorry I wasn't clear. setting $("title").text()/html() doesn't work. IE8 complains about "IE8 - Unexpected call to method or property access".
Ivo van der Wijk
I will update my answer for a working one in IE 8
Starx
+1  A: 

Assuming you're using the latest jQuery, doing something as simple as:

$('title').text('My new title');

should work. At least, this works doing a simple in-page javascript console test in google Chrome. You could use .html instead of .text, but generally you don't want HTML in the title tag, since that's not usually allowed and might display weirdly - with .text at least you know your new title string will be escaped and not lead to any weird behaviour.

Otherwise I expect doing something using straight javascript would be fine, such as:

document.title = 'A new title';
darkliquid
heh, console in Chrome... exactly what I did :)
Carson Myers
A: 
<script type="text/javascript">
      $(document).ready(function() {

        document.title = 'blah';

      });
    </script>

also check this http://hancic.info/change-page-title-with-jquery

Space Cracker
+1  A: 
document.title = "newtitle" 

is the only valid way as far as I know. manipulating

$("title") 

will fail on IE8.

There are subtle differences between the title tag and document.title, it appears browsers treat them differently.

Ivo van der Wijk