Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the title of the document during .ready()?

People also ask

How do I change the title of a JavaScript document?

Use the querySelector() Method to Change the Page Title in JavaScript. We can use the document. querySelector() method to pick elements in a document. The title element can be chosen by giving the title element a selector parameter and retrieving the page's main title element.

What is the purpose of $( document ready ()?

The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.

Can we use document ready multiple times?

Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.

What is $( document ready () equivalent in JavaScript?

jQuery $(document). ready() Equivalent in JavaScriptThis event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.


The following should work but it wouldn't be SEO compatible. It's best to put the title in the title tag.

<script type="text/javascript">

    $(document).ready(function() {
        document.title = 'blah';
    });

</script>

Do not use $('title').text('hi'), because IE doesn't support it.

It is better to use document.title = 'new title';


This works fine in all browser...

$(document).attr("title", "New Title");

Works in IE too


Like this:

$(document).ready(function ()
{
    document.title = "Hello World!";
});

Be sure to set a default-title if you want your site to be properly indexed by search-engines.

A little tip:

$(function ()
{
    // this is a shorthand for the whole document-ready thing
    // In my opinion, it's more readable 
});

<script type="text/javascript">
$(document).ready(function() {

    $(this).attr("title", "sometitle");

});
</script>

document.title was not working for me.

Here is another way to do it using JQuery

$('html head').find('title').text("My New Page Title");