admin管理员组

文章数量:1287115

Suppose I have a URL like www.google, 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?

Suppose I have a URL like www.google., 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?

Share Improve this question edited Oct 25, 2012 at 13:57 John asked Oct 25, 2012 at 13:49 JohnJohn 4,70612 gold badges40 silver badges43 bronze badges 4
  • 1 see stackoverflow./questions/9228947/… – Amol Kolekar Commented Oct 25, 2012 at 13:52
  • 1 Are You loading the URL in the browser, or just want to know the title for it? – undefined Commented Oct 25, 2012 at 13:52
  • 1 @xyu I just want to know the title for it. – John Commented Oct 25, 2012 at 13:55
  • 4 As noted by Xander in his answer, if the page you're wanting to look into is not in the same domain as your page (and I'm pretty sure www.google. isn't!!) then you will hit the Same Origin Policy, which will remove your ability to do this on the browser itself – freefaller Commented Oct 25, 2012 at 14:00
Add a ment  | 

5 Answers 5

Reset to default 8

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 pares, performance-wise, against the other options here:

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

本文标签: jqueryExtract HTML title in JavascriptStack Overflow