Re: [問題] 讀取外部title
※ 引述《driftseed (阿輝)》之銘言:
: 檔案:http://www.sendspace.com/file/togq72 (959B)
: 截圖:http://i.imgur.com/e4xl6.jpg
: 在javascript想要得到一個"外部"網站的標題內容不知要從何下手
: 外部網頁僅透過
: xmlhttp.Open("GET",URL, false);
: 來測試網址是否有效,並不打算在框架中載入那個網頁
: 不過xmlhttp.Open測試的時候也算是一種載入網頁吧?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"" rel="nofollow">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="" rel="nofollow">http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>測試</title>
<script type="text/javascript">
function request(url, callback, method)
{
var xmlHttp = createXMLHTTPObject();
if ( ! xmlHttp) return;
xmlHttp.open(method, url, true);
xmlHttp.onreadystatechange = function ()
{
if (xmlHttp.readyState != 4) return;
if (xmlHttp.status != 200 && xmlHttp.status != 304)
{
alert('HTTP請求錯誤 ' + xmlHttp.status);
return;
}
callback(xmlHttp);
}
if (xmlHttp.readyState == 4) return;
xmlHttp.send(null);
}
function createXMLHTTPObject()
{
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")},
];
var xmlhttp = false;
for (var i = 0; i < XMLHttpFactories.length; i ++ )
{
try
{
xmlhttp = XMLHttpFactories[i]();
}
catch (e)
{
continue;
}
break;
}
return xmlhttp;
}
function checkUrl(url)
{
try
{
if(url)
{
request(url, function (response)
{
document.getElementById("responseHere").innerHTML=response.responseText.match(/(<title>).*<\/title>/gi)[0].replace(/<.?title>/gi,"");
}, "GET");
}
else
throw "Url is undefined";
}
catch(e)
{
document.getElementById("responseHere").innerHTML=((e) ||
(e.description));
}
}
</script>
</head>
<body>
<form>
輸入網址:
<input type="text" id="txtUrl" value="http://tw.yahoo.com/" />
<input type="submit" value="test"
onclick="checkUrl(document.getElementById('txtUrl').value);return false;" />
</form>
<ul>
<li>測試網址是否有效 </li>
<li>有效--得到該網址的標題--結束</li>
<li>無效--結束</li>
</ul>
<div id="responseHere"></div>
</body>
</html>
奇怪的是 firefox 與 chrome 都不支援 到底是哪裡出問題...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.172.94.13
→
07/30 14:19, , 1F
07/30 14:19, 1F
→
07/30 14:19, , 2F
07/30 14:19, 2F
→
07/30 14:20, , 3F
07/30 14:20, 3F
推
07/30 18:14, , 4F
07/30 18:14, 4F
→
07/30 18:18, , 5F
07/30 18:18, 5F
→
07/30 18:19, , 6F
07/30 18:19, 6F
→
07/30 18:19, , 7F
07/30 18:19, 7F
→
07/30 18:22, , 8F
07/30 18:22, 8F
→
08/09 07:14, , 9F
08/09 07:14, 9F
→
08/09 07:16, , 10F
08/09 07:16, 10F
推
08/10 20:51, , 11F
08/10 20:51, 11F
→
08/10 20:51, , 12F
08/10 20:51, 12F
→
08/10 20:51, , 13F
08/10 20:51, 13F
→
08/10 21:10, , 14F
08/10 21:10, 14F
→
08/10 21:10, , 15F
08/10 21:10, 15F
→
08/10 21:10, , 16F
08/10 21:10, 16F
討論串 (同標題文章)