SoFunction
Updated on 2025-03-04

JS implements a method to detect website links [test available]

This article describes the method of detecting website links by JS. Share it for your reference, as follows:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jsDetect website links</title>
</head>
<body>
<div ></div>
<br/>
<div ></div>
<script language="JavaScript">
function butt(){
  tim=1;
  setInterval("tim++",100);
  autourl1="http://"
  autourl2="https://"
  var url1=("url1");
  var url2=("url2");
  =" Test connection: http://, testing...";  =" Test connection: https://, testing...";}
function auto(url){
  if(tim>20) {
    (url).innerHTML=" Link Timed out "
  }else{
    (url).innerHTML="Linking time:"+tim/10+"Second "
  }
  //b++
}
function run(){
  ("<img src="+autourl1+"/"+()+" width=0 height=0 onerror=auto('url1')>")
  ("<img src="+autourl2+"/"+()+" width=0 height=0 onerror=auto('url2')>")
}
butt()
run()
</script>
</body>
</html>

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

For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript window operations and techniques》、《JavaScript traversal algorithm and skills summary》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation special effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.