Reputation: 5413
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
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
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
Reputation: 2014
maybe ajax can do this, but by primitive tools, you cant! (js is client side!)
Upvotes: 2