4

Is there any method that will return the title of the web page given its URL?

I don't mean the title of the current page which can be gotten from doing document.title, I mean getting the title of another web page.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
k.ken
  • 4,973
  • 5
  • 23
  • 21

4 Answers4

5

You can't access this is the site is on a different domain. That would be a security violation of the same origin policy.

Otherwise, you could load the other web page into a hidden iframe and use this:

document.getElementById('myIframe').contentWindow.document.title;

Actually I semi-lied. It is a security violation to do this, but IE has a bug whereby you can access the content via VBScript. Not sure if this has been patched yet.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • In any browser this should work for same domain: `var x = window.open('url'); var title = x.document.title; x.close();` It may trigger popup blockers though. – Paul Jun 07 '12 at 21:47
  • Yeah that's why I prefer iframes. – tskuzzy Jun 07 '12 at 21:48
  • which will save time? Navigating to the URL and then getting the title or doing as you said above? – k.ken Jun 07 '12 at 22:05
3

No.

No you cannot.

Not without some extra APIs

Naftali
  • 144,921
  • 39
  • 244
  • 303
2

maybe ajax can do this, but by primitive tools, you cant! (js is client side!)

-1

Yes you can and its very simple.

chrome.topSites.get(function(info){ //fetching the top sites visited in chrome
    var xyz=document.getElementsByClassName('urclassname');
    for(var i=0;i<info.length;i++) {
       console.log(info[i]);  //check the data fetched by info
       xyz[i].setAttribute("urclassattribute",info[i].title); //set the title of the fetched url
    }
});

Suppose my top visited site is Facebook, so the console.log(info[i]) will give the value in the browser console as:

Object

title : "(3) Facebook" url : "https://www.facebook.com/" proto : constructor : ƒ Object() hasOwnProperty : ƒ hasOwnProperty() isPrototypeOf : ƒ isPrototypeOf() propertyIsEnumerable : ƒ propertyIsEnumerable() toLocaleString : ƒ toLocaleString() toString : ƒ toString() valueOf : ƒ valueOf() defineGetter : ƒ defineGetter() defineSetter : ƒ defineSetter() lookupGetter : ƒ lookupGetter() lookupSetter : ƒ lookupSetter() get proto : ƒ proto() set proto : ƒ proto()

So basically you get the title of the website.

Nils Lindemann
  • 1,146
  • 1
  • 16
  • 26
Megha Verma
  • 141
  • 1
  • 3
  • 12