2012-06-07 86 views
2

是否有任何方法會根據給定的URL返回網頁的標題?獲取給定網址的網頁標題

我不是指可以從document.title得到的當前頁面的標題,我的意思是獲得另一個網頁的標題。

+0

什麼是「URL的標題」? – FishBasketGordo

+4

Sir'URL'? ..... – vascowhite

+0

可能是URL指向的網頁的標題。 – tskuzzy

回答

3

不,你不能。

不是沒有一些額外的API

+0

+1,因爲我喜歡那些不是真正問題的答案! – gdoron

+1

是的,如果它在同一個域中是可能的。 –

4

您不能訪問,這是該網站是在不同的領域。這將是對same origin policy的安全違規。

否則,您可以在其他網頁加載到一個隱藏iframe並使用此:

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

其實我半撒謊。這樣做會違反安全規定,但IE有一個錯誤,您可以通過VBScript訪問內容。不確定這是否已被修補。

+0

在任何瀏覽器中,這應該適用於相同的域:'var x = window.open('url'); var title = x.document.title; x.close();'它可能會觸發彈出窗口阻止程序。 – Paulpro

+0

是的,這就是我喜歡iframe的原因。 – tskuzzy

+0

你說使用iframe不起作用? – Paulpro

2

也許阿賈克斯可以做到這一點,但通過原始工具,你不能! (js是客戶端!)

0

是的,你可以和它非常簡單。

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

假設我最喜歡的網站是Facebook,所以控制檯。日誌(信息[1])將在瀏覽器控制檯給出了值:

對象

標題 : 「(3)Facebook的」 網址 : 「https://www.facebook.com/ : 構造 : ƒ對象() hasOwnProperty : ƒhasOwnProperty() isPrototypeOf : ƒisPrototypeOf() propertyIsEnumerable : ƒpropertyIsEnumerable() 的toLocaleString : ƒ的toLocaleString() 的toString : ƒ的toString() 的valueOf : ƒ的valueOf() defineGetter : ƒ defineGetter() defineSetter : ƒdefineSetter() lookupGetter : ƒlookupGetter() lookupSetter : ƒlookupSetter() 得到 : ƒ() 設置 : ƒproto()

所以基本上你會得到網站的標題。