유효한 링크인지 확인하는 JAVASCRIPT

by 조쉬 posted Jan 16, 2019
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄
<HTML>
<HEAD>
<TITLE>link status check</TITLE>
<SCRIPT>
function fetchHTTPStatus (url) {
  if ((location.host == '') || (url.indexOf(location.host) == -1))
    netscape.security.PrivilegeManager.enablePrivilege('UniversalConnect');
  var urlObj = new java.net.URL (url);
  var host = urlObj.getHost();
  var port = (urlObj.getPort() > 0) ? urlObj.getPort() : 80;
  var fileName = urlObj.getFile();
  var sock = new java.net.Socket (host, port);
  dock = new java.io.DataOutputStream(sock.getOutputStream());
  dock.writeBytes('HEAD ' + fileName + ' HTTP/1.0\r\n'); 
  dock.writeBytes('\r\n');
  var dis = new java.io.DataInputStream(sock.getInputStream());
  line = dis.readLine(); // get just status message
  dis.close();
  dock.close();
  sock.close();
  var statusObject = new StatusObject(line);
  return statusObject;
}


function StatusObject (statusLine) {
  var re = /(HTTP\/[\d\.]+) (\d\d\d) ?(.*)$/i;
  var match = re.exec(statusLine);
  this.protocol = match[1];
  this.status = match[2];
  this.message = match[3];
}


function checkURLStatus (url) {
  if (document.getElementById && window.ActiveXObject) {
    var http = new ActiveXObject('Microsoft.XMLHTTP');
    http.open('HEAD', url, false);
    http.send();
    return http.status;
  }
  else if (document.layers && navigator.javaEnabled()) {
    return fetchHTTPStatus(url).status
  }
  else 
    return null;
}


function checkLinks () {
  for (var l = 0; l < document.links.length; l++) {
    window.status = 'checking link ' + l;
    var link = document.links[l];
    var status = checkURLStatus(link.href);
    if (status) {
      if (document.all) {
        if (status == 200)
          link.insertAdjacentHTML('afterEnd', '<SUP><SPAN STYLE="color: green;">' + status + '<\/SPAN></SUP>');
        else
          link.insertAdjacentHTML('afterEnd', '<SUP><SPAN STYLE="color: red;">' + status + '<\/SPAN></SUP>');
      }
      else if (document.layers) {
        link.ol = new Layer(20);
        link.ol.document.open();
        if (status == 200)
          link.ol.document.write('<FONT COLOR="green">' + status + '<\/FONT>');
        else
          link.ol.document.write('<FONT COLOR="red">' + status + '<\/FONT>');
        link.ol.document.close();
        link.onmouseover = showStatus;
        link.onmouseout = hideStatus;
      }
    }
  }
  window.status = '';
}


function showStatus (evt) {
  evt.target.ol.left = evt.pageX;
  evt.target.ol.top = evt.pageY;
  evt.target.ol.visibility = 'show';
}


function hideStatus (evt) {
  evt.target.ol.visibility = 'hide';
}
</SCRIPT>
</HEAD>


<BODY>
<input type=button name=bt value=링크체크하기 onclick=checkLinks()>
<br><br>
  <A HREF="http://www.kibo.com">Kibo</A>
| <A HREF="http://www.kibo.com/god">god</A>
| <A HREF="http://www.faqts.com/js">js</A>
| <A HREF="http://www.faqts.com">faqts</A>
</BODY>
</HTML>