67

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.

12
  • In Google Chrome there's no problem. Are you sure that the content of <title> tag doesn't contain "- Mozilla Firefox"? Commented Feb 10, 2012 at 14:06
  • 2
    I do not get the vendor name in any browser with document.title, unless it is included between the title tags. Commented Feb 10, 2012 at 14:13
  • 5
    DO NOT USE JQUERY FOR THIS! document.title is the correct approach and 'mozilla firefox' will for sure not be in that value - ever! Commented Feb 10, 2012 at 14:28
  • 1
    @mikkom This is also Ubuntu, FF 10, and I have never seen " - Mozilla Firefox" prefixed after the title in document.title, not even in earlier versions of FF. Commented Feb 10, 2012 at 15:29
  • 1
    @Zenexer its not about the load of the lib. Its about using jquery for something, for which javascript "was designed". Jquery is great, but in this case it just adds a shitload of cpu-cycles Commented Feb 11, 2012 at 13:28

7 Answers 7

139

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;
Sign up to request clarification or add additional context in comments.

8 Comments

do not use jquery for such a trivial problem.
Added same solution in standard JS
@munchschair since document.getElementsByTagName() returns an array of elements, [0] selects the first element in that array.
@comfytoday I agree about defaulting to document.title. But the question is explicitly for situations where document.title returns unwanted results (eg. extra characters), and the person asking the question doesn't want to parse the returned title.
|
9

try like this

$('title').text();

2 Comments

Already tried. Doesn't work (returns empty string for some reason).
@mikkom: Was your <title> in <head>? Did you try it on any other versions of Firefox at the time? Did the HTML validate?
6
$('title').text();

returns all the title

but if you just want the page title then use

document.title

Comments

4

Like this :

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

works for IE, Firefox and Chrome.

Comments

1

You can get it with plain JavaScript DOM methods.The concept is easy:

  1. Retrieve title element from DOM.

  2. Get its content using innerHTML or innerText.

So:

const titleElement = document.getElementsByTagName("title")
const title = titleElement.innerText

console.log(title) // The title of the HTML page.

To retrieve, you can use other methods such as querySelector, or adding an id to title, getElementById.

Comments

0

To get title and save it to a constant use:

const { title } = document;

Comments

-1

Retro code. It was the Internet Explorer way:

document.all.tags('title')[0].text;

Works as deep as Internet Explorer 4.0.

2 Comments

Don't spam the same (terrible) answer onto multiple questions. If you think two questions are duplicates then, when you have some more reputation, you can vote to close one of them with a pointer to the other.
@Quentin I've replied to you in the other place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.