-5

I want to change the title of page dynamically. I have lots of AJAX request going on in my page. On each type of response I want to notify this using the title.

So, How to change the title of page through jQuery?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
Starx
  • 77,474
  • 47
  • 185
  • 261
  • How to change the favicon then? – Starx Aug 01 '10 at 09:17
  • Just 12 minutes and desesperated? Google is faster. This is more personalized but slower. – Jesus Rodriguez Aug 01 '10 at 09:19
  • You don't get this do you: http://api.jquery.com. Read and learn. We're here to help you, not do it for you. You cannot change the favicon after the page has loaded, well, you can (`$('link[rel="shortcut icon"]').attr('href', 'favicon.png');`), but it won't have any effect. – Matt Aug 01 '10 at 09:21
  • favicon... not sure that can be done with JS.. at least in all browsers. sure someone will correct me if I'm wrong. at least the file should be favicon.ico be default in your sites root. anything else will be specified by in the header. I don't think changing this in IE works – WalterJ89 Aug 01 '10 at 09:25
  • http://stackoverflow.com/questions/260857/changing-website-favicon-dynamically – WalterJ89 Aug 01 '10 at 09:26

7 Answers7

17
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
  • 16,341
  • 4
  • 43
  • 57
15

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
  • 377,238
  • 77
  • 533
  • 578
3
$(document).attr("title", "New Title");
Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
Meisam
  • 41
  • 1
3
$('title').html('newTitle')
sTodorov
  • 5,435
  • 5
  • 35
  • 55
2

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
  • 442,112
  • 142
  • 972
  • 1,088
  • It appears that caveat only applies to XUL, at least as far as that document mentions. – Carson Myers Aug 01 '10 at 09:17
  • @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 Aug 01 '10 at 09:21
2
<script type="text/javascript">
      $(document).ready(function() {

        document.title = 'blah';

      });
    </script>

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

Amr Badawy
  • 7,453
  • 12
  • 49
  • 84
1

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
  • 4,003
  • 1
  • 25
  • 17