Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current html page title with javascript

I'm trying to get the plain html page title with javascript.

I use firefox and with

document.title 

I get extra "- Mozilla Firefox" to the end of the title. I know it would be easy to get rid of this by modifying string but if they change text, use different format etc or some other browser modifies this differently I have extra text there again.

So, is there any cross browser way to get the plain tag content with javascript? Jquery solution is ok.

like image 602
mikkom Avatar asked Feb 10 '12 14:02

mikkom


People also ask

How do I get the page title in HTML?

The <title> tag defines the title of the document. The title must be text-only, and it is shown in the browser's title bar or in the page's tab. The <title> tag is required in HTML documents!

How do you change the title of a web page using Javascript?

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.

How do you find the title of a web page?

If you have trouble finding the “<title>” in the sea of HTML, then use the Find function. Again, on Windows, you can select Ctrl + F and then type “title” to quickly find the Title. That's all there is to it. Now you can easily find the webpage Title for any page on your website.

How do you create a dynamic title in HTML?

If you are loading via ajax and you want to dynamically change the page title with just Javascript, then do: document. title = 'Put the new title here'; However, search engines will not see this change made in javascript.


3 Answers

One option from DOM directly:

$(document).find("title").text();

Tested only on chrome & IE9, but logically should work on all browsers.

Or more generic

var title = document.getElementsByTagName("title")[0].innerHTML;
like image 172
Marcus Avatar answered Oct 26 '22 15:10

Marcus


try like this

$('title').text();
like image 35
Mike Avatar answered Oct 26 '22 13:10

Mike


Like this :

jQuery(document).ready(function () {
    var title = jQuery(this).attr('title');
});

works for IE, Firefox and Chrome.

like image 4
JuSchz Avatar answered Oct 26 '22 13:10

JuSchz