English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Implementation Method for Checking if the Requested URL is Accessible, Supporting Cross-Domain Checking

As shown below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <meta name="keywords" content="js judge URL accessibility" />
  <title>js judge URL accessibility</title>
</head>
<body>
  <div>URL to Test: </div>
  <input type="text" style="width:600px;height:30px;font-size:14px;" id="urlText" value="https://www.baidu.com/" />
    <input type="button" value="Check if Accessible" onclick="getURL()" />
  <br />
  <div id="msg1></div>
  <div id="msg"></div>
  <script type="text/javascript" src="js/jquery-1.10.2script"></script>
  <script type="text/javascript">
    function getURL() {
      $("#msg").html("");
      var url = $("#urlText").val();//The requested URL
      var dateTime = disptime();
      var time2 = dateTime.DateTime; 
      $("#msg1").html("Sending Time: " + time2); 
      $.ajax({
        type: 'get',
        url: url,
        cache: false,
        dataType: "jsonp", //Cross-domain uses jsonp method 
        processData: false,
        timeout:10000, //Timeout time, in milliseconds
        complete: function (data) {
          var dateTime2 = disptime();
          var time22 = dateTime2.DateTime;
          var htmlTxt =[];
          if (data.status==200) {
            htmlTxt.push("Success<br/)";
          } else {
            htmlTxt.push("Failed<br/)";
          }        
          htmlTxt.push("readyState=" + data.readyState + "<br/>status=" + data.status + "<br/>statusText=" + data.statusText + "<br/>Response Time: " + time22);
          var htmlString = htmlTxt.join('');
          $("#msg").html(htmlString);
        }       
      });
    }
    function disptime() {
      var date = new Date();
      var yyyy = date.getFullYear();//four-digit year
      var month = date.getMonth() + 1;//month 0-11
      var day = date.getDate();//date
      var HH = date.getHours();//hours
      var minute = date.getMinutes();//minutes
      var second = date.getSeconds();//seconds
      var milliseconds=date.getMilliseconds();//milliseconds
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      if (HH < 10) {
        HH = "0" + HH;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      if (second < 10) {
        second = "0" + second;
      }
      var time = yyyy + "-" + month + "-" + day + " " + HH + : + minute + : + second + " " + milliseconds;
      var timeTxt = yyyy + month + day + HH + minute + second;
      var time = {
        DateTime: time,
        TimeTxt: timeTxt
      }
      return time;
    }
  </script>
</body>
</html> 

The above article about whether the URL of the js request is accessible, the implementation method of cross-domain judgment shared by the editor is all the content I want to share with everyone. I hope it can be a reference for everyone, and I also hope everyone will support the Yelling Tutorial more.

You May Also Like