Niklaus
Niklaus

Reputation: 65

How to show an alert in case of HTTP error 404.3

I'm trying open a document in window.open() which throws

HTTP error 404.3

Now I know the solution with IIS Settings or setting staticContent in web.config. But I just want to show an alert in case of 404.3 error.

Upvotes: 0

Views: 323

Answers (3)

Niklaus
Niklaus

Reputation: 65

I found another solution which worked for me.

            var XMLHttp = new XMLHttpRequest();
            XMLHttp.open("GET", "" + url + "");
            XMLHttp.onreadystatechange = handlerFunction;
            XMLHttp.send(null);
            function handlerFunction() {
                if (XMLHttp.readyState == 4 && XMLHttp.status == 404) {
                    alert("File not reachable");
                }
                else if (XMLHttp.readyState == 4 && XMLHttp.status !== 404) {
                    window.open(url, '_blank');
                }
            }

Upvotes: 1

Aria
Aria

Reputation: 3844

It is depend on your scenario how you want to interpret Error type in error page.

As you know Error 404.3 happened in server side (IIS) not in client side so it must be controlled in IIS web.config or something in back-end, but in easy way you can redirect user to a page with specific query string to error page and in error page control query string !!

For example take a look at this:

<customErrors mode="On" defaultRedirect="LogIn.aspx" redirectMode="ResponseRewrite">
  <error statusCode="403" redirect="ErrorPages/Oops.html?Error=NoAccess"/>
  <error statusCode="404" redirect="ErrorPages/Oops.html?Error=FileNotFound"/>
  <error statusCode="500" redirect="ErrorPages/Oops.html?Error=ServerError"/>
</customErrors>

in Oops.html read query string and show your alert:

  function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

and in load:

  switch (getParameterByName("Error")) {
    case 'NoAccess':
        alert('The page you are looking for is not exist in the system.');
        break;
    case "FileNotFound":
        alert('File not found.');
        break;
    case "ServerError":
        alert('Error in server.');
        break;
    default:
        break
}

Upvotes: 0

Ferus7
Ferus7

Reputation: 727

If you get a 404.3 error, your code is not going to be interpreted, so IIS Settings is the solution here

Upvotes: 0

Related Questions