12

I have some html in string form.

var html = "
<html>
  <head>
    <title>
      Some Text
    </title>
  </head>
  <body>
    <h1>
      My First Heading
    </h1>
    <p>
      My first paragraph.
    </p>
  </body>
</html>
";

It has title tag inside it. How can I get the text inside title tag using jquery or javascript?

Sushil Kumar
  • 1,401
  • 2
  • 14
  • 27

10 Answers10

31

Just try with:

var title = $(html).filter('title').text();
hsz
  • 148,279
  • 62
  • 259
  • 315
7

Just because no one mentioned it so far, you could also use document.implementation.createHTMLDocument() for that purpose.

var domstr = "<html><head><title>Some Text</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";
    doc    = document.implementation.createHTMLDocument('1337');
    doc.documentElement.innerHTML = domstr;

alert( doc.title );
jAndy
  • 231,737
  • 57
  • 305
  • 359
5

This is my crack at it. Create a documentFragment add an element to it and use querySelector to get the element and then textContent to get the text.

var html = "<html><head><title>Some Text</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>",
    docFrag = document.createDocumentFragment(),
    el = document.createElement('html');

el.innerHTML = html;
docFrag.appendChild(el);

var text = docFrag.querySelector('title').textContent;

Live Demo

Loktar
  • 34,764
  • 7
  • 90
  • 104
4

How about using the DOMparser

var html  = "<html><head><title>Some Text</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>",
    doc   = (new DOMParser()).parseFromString(html, 'text/html');
    title = doc.title;

Note: This is not supported on any version of Safari

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
4

In JavaScript, simply

document.title

For sample code

<html>
    <head>
        <title>My Page Title</title>
    </head>
    <body>
        <p id="demo"></p>
        <script>
            document.getElementById("demo").innerHTML = document.title;
        </script>
    </body>
</html>
saiid
  • 635
  • 1
  • 6
  • 20
1
html.match(/<title>(.*)<\/title>/)[1]
griffon vulture
  • 6,594
  • 6
  • 36
  • 57
1

Obviously not preferred if you can use jQuery, but for pure Javascript:

/<title>(.*)<\/title>/.exec(html)[1]
ach
  • 6,164
  • 1
  • 25
  • 28
  • http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not – epascarello May 15 '14 at 14:52
  • 1
    Yeah, we get it -- pasting that link once per question is probably enough. I even stated that the jQuery answer is preferred, I was just trying to provide a pure Javascript alternative. Thanks for the downvote though. – ach May 15 '14 at 15:22
  • @Ach your answer isn't "acceptible" it isn't even functional for other cases of parsing HTML. The question here is how to extract data from an HTML string based on tags. Your answer works in this one specific case, but is worthless and guarantees problems. Your answer should be deleted. Downvote is from me. –  May 15 '14 at 16:27
0
html.substring(html.indexOf('<title>')+7, html.indexOf('</title>'))
Jalay
  • 66
  • 1
  • 7
0

You can get that text by using this jQuery selector.

$(function(){
var html = "<html><head><title>Some Text</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";
var titleHTML = $(html).find("title").html();
alert(titleHTML);
});

It will alert "Some Text"

Ramesh
  • 1,829
  • 2
  • 25
  • 30
0

You can also try this:

$('head > title').text();
Fahim Hossain
  • 1,671
  • 13
  • 16