Home  >  Article  >  Web Front-end  >  JS method to implement detection of website links

JS method to implement detection of website links

高洛峰
高洛峰Original
2016-12-07 13:56:021149browse

The example in this article describes the method of detecting website links using JS. Share it with everyone for your reference, the details are as follows:

If you want to detect whether a website link exists through a program, you can do it through server-side high-level language programming. In fact, client-side JavaScript can also be detected. Here is a trick to detect website links.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js探测网站链接</title>
</head>
<body>
<div id="url1"></div>
<br/>
<div id="url2"></div>
<script language="JavaScript">
function butt(){
  tim=1;
  setInterval("tim++",100);
  autourl1="http://www.163.com"
  autourl2="http://www.jb51.net"
  var url1=document.getElementById("url1");
  var url2=document.getElementById("url2");
  url1.innerHTML=" 测试连接:http://www.163.com,测试中..... ";
  url2.innerHTML=" 测试连接:http://www.jb51.net,测试中..... ";
}
function auto(url){
  if(tim>20) {
    document.all(url).innerHTML=" 链接超时 "
  }else{
    document.all(url).innerHTML=" 链接耗时:"+tim/10+"秒 "
  }
  //b++
}
function run(){
  document.write("<img src="+autourl1+"/"+Math.random()+" width=0 height=0 onerror=auto(&#39;url1&#39;)>")
  document.write("<img src="+autourl2+"/"+Math.random()+" width=0 height=0 onerror=auto(&#39;url2&#39;)>")
}
butt()
run()
</script>
</body>
</html>

The principle of the above code is to load an image with a length and width of 0. Of course, this image does not exist, so an error response will be returned to trigger the onerror time. Based on the return time, the accessible link is determined


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn