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.
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.
Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With