k.ken
k.ken

Reputation: 5413

Getting the title of a web page given the URL

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.

Upvotes: 4

Views: 4974

Answers (4)

Megha Verma
Megha Verma

Reputation: 151

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.

Upvotes: -1

tskuzzy
tskuzzy

Reputation: 36446

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.

Upvotes: 5

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

Upvotes: 2

Naftali
Naftali

Reputation: 146302

No.

No you cannot.

Not without some extra APIs

Upvotes: 3

Related Questions