在站长工具中,经常看到判断一个页面的关键词密度,他是怎么实现的获取外部网页内容的呢。是通过js的ajax,获取后再计算。
下面是获取外部网页代码的代码。也可以查看效果:javascript读取网页代码
下面是详细代码
function check(url)
{
var xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
var chkUrl = url;
xmlHttp.open('get', chkUrl, false);
try{
xmlHttp.send();
}
catch(e){
window.alert('出现异常');
}
oSource.value=xmlHttp.responseText;
// alert(bytes2BSTR(xmlHttp.responseText));
fn.document.body.innerHTML="
}
Function getHTTPPage(url)
on error resume next
dim http,code
set http= createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
code=bytes2BSTR(Http.responseBody)
oSource.value=code
fn.document.body.innerHTML="
set http=nothing
if err.number<>0 then err.Clear
End function
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
更多信息请查看IT技术专栏