Ad

Javascript Check The Address Path Link Is Available Or Not

- 1 answer

<script>
  function doSomethingIfFound(url, status) {
    alert(url + " was found, status" + status);
  }

  function doSomethingIfNotFound(url, status) {
    alert(url + " was not found, status" + status);
  }

  function test(url) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
        doSomethingIfFound(url, xhttp.status);
      } else if (xhttp.readyState == 4 && xhttp.status != 200) {
        doSomethingIfNotFound(url, xhttp.status);
      }
    };
    xhttp.open("GET", url, true);
    xhttp.send();
  }


  test('https://test.e-cover.com.my/')
  test('https://www.e-cover.com.my/')
  test('http://www.mypolicy.com.my/mypolicy/login.jsp/')
  test('https://itdidnotexistwhenitrieitiswear.museum/')
</script>

I have no idea why my code detect some url links are available and some are not available although the so called "not available" are available. Please help, thanks.

<script>
  function checkURL(url) {
    var scriptTag = document.body.appendChild(document.createElement("script"));
    scriptTag.onload = function() {
      alert(url + " is available");
    };
    scriptTag.onerror = function() {
      alert(url + " is not available");
    };
    scriptTag.src = url;
  }
</script>

<body>

  link 1 available site detected as available (correct output) :
  <input type="radio" name="link" onclick="checkURL('https://www.e-cover.com.my/');">
  <br/>link 2 available site but detected as not available :
  <input type="radio" name="link" onclick="checkURL('https://test.e-cover.com.my/');">
  <br/>link 3 available site but detected as not available :
  <input type="radio" name="link" onclick="checkURL('http://www.mypolicy.com.my/mypolicy/login.jsp/');">
  <br/>

</body>

Ad

Answer

The idea is neat but you cannot do it that way, you need to do it properly, sorry. Just do an AJAX request and ignore the return, just check the HTTP codes.

function doSomethingIfFound(url, status){
  console.log(url + " was found, status" + status);
}
function doSomethingIfNotFound(url, status){
  console.log(url + " was not found, status" + status);
}
function test(url) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      doSomethingIfFound(url, xhttp.status);
    } else if ( xhttp.readyState == 4 && xhttp.status != 200 ){
      doSomethingIfNotFound(url, xhttp.status);
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
}


test("https://test.e-cover.com.my/")
test("https://www.e-cover.com.my/")
test("http://www.mypolicy.com.my/mypolicy/login.jsp/")
test("https://itdidnotexistwhenitrieitiswear.museum/")

gives the results

"https://test.e-cover.com.my/ was found, status 200"
"https://itdidnotexistwhenitrieitiswear.museum/ was not found, status 0"
"https://www.e-cover.com.my/ was found, status 200"
"http://www.mypolicy.com.my/mypolicy/login.jsp/ was not found, status 404"

So, if the url does not exists at all, the request returns 0, 404 if the site exists but has not the page you want and 200 if all is ok and proper.

If that still doesn't work for you, you need to be a bit more precise in the details.

Ad
source: stackoverflow.com
Ad