简体   繁体   中英

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?

given the url is of the same origin:

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

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;
});
$.get(url, function(html) {
   var title =  $(html).title
});

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;
});

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM