John
John

Reputation: 4706

Extract HTML title in Javascript

Suppose I have a URL like www.google.com, and I want Javascript/JQuery on my page to go see what is in the <title> for the content of that URL, and put it into a variable for me. How can I make it work?

Upvotes: 3

Views: 923

Answers (5)

crmpicco
crmpicco

Reputation: 17181

This will also work for you. I'm not sure how a match compares, performance-wise, against the other options here:

var html_title = html.match("<title>(.*?)</title>")[1];

Upvotes: 1

DarkAjax
DarkAjax

Reputation: 16223

You can try something like:

var title = document.title;

After seeing your edited question I guess what you wanted as the others remark is:

jQuery.get('<url of the page you want>', function(data) {
    var title =  jQuery(data).title;
});

Upvotes: 4

NullPoiиteя
NullPoiиteя

Reputation: 57322

try

var titles    = document.getElementsByTagName('title')

try to det title from url

$.get(document.URL, function(demo) { //or window.location.href to get the url of currec\nt page 
    var title = $(demo).title;
});

Upvotes: 2

Liam
Liam

Reputation: 29694

$.get(url, function(html) {
   var title =  $(html).title
});

Upvotes: 3

Alex
Alex

Reputation: 35409

given the url is of the same origin:

$.get('page.html', function(text) {
    var pagetitle = $(text).title;
});

Upvotes: 8

Related Questions