JS实现探测网站链接的方法【测试可用】
本文实例讲述了JS实现探测网站链接的方法。分享给大家供大家参考,具体如下:
如果想通过程序探测一个网站链接是否存在,可以通过服务器端高级语言编程实现,其实客户端的JavaScript也是可以探测的,下面通过一个技巧来探测网站链接。
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>js探测网站链接</title>
</head>
<body>
<divid="url1"></div>
<br/>
<divid="url2"></div>
<scriptlanguage="JavaScript">
functionbutt(){
tim=1;
setInterval("tim++",100);
autourl1="http://www.163.com"
autourl2="https://www.nhooo.com"
varurl1=document.getElementById("url1");
varurl2=document.getElementById("url2");
url1.innerHTML="测试连接:http://www.163.com,测试中.....";
url2.innerHTML="测试连接:https://www.nhooo.com,测试中.....";
}
functionauto(url){
if(tim>20){
document.all(url).innerHTML="链接超时"
}else{
document.all(url).innerHTML="链接耗时:"+tim/10+"秒"
}
//b++
}
functionrun(){
document.write("<imgsrc="+autourl1+"/"+Math.random()+"width=0height=0onerror=auto('url1')>")
document.write("<imgsrc="+autourl2+"/"+Math.random()+"width=0height=0onerror=auto('url2')>")
}
butt()
run()
</script>
</body>
</html>
上面代码原理是加载一个长宽为0的图片,当然这个图片是不存在的,所以会返回错误响应触发onerror时间,根据返回时间来确定是可以访问的链接
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript窗口操作与技巧汇总》、《JavaScript遍历算法与技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。