Vadim
Vadim

Reputation: 21724

404 detection in JavaScript

In my JavaScript I'm trying to redirect to third party page. It can open a page either in a new window or inside a frame depends on a user settings. Something like this:

if (newWindow)
{
   window.open(url, targer);
}
else
{
   theFrame = url;
}

What I want to do is to display my custom page in case a third party site is down or page is unavailable. Basically in case of 404 error.

What's the best way to solve this problem?

Upvotes: 0

Views: 478

Answers (3)

Vadim
Vadim

Reputation: 21724

Following @risyasin advise I solved my problem on the server side using ASP.NET.

protected void Page_Load(object sender, EventArgs e) {
    HttpWebResponse response;
    try
    {
        var request =
            (HttpWebRequest)WebRequest.Create("http://www.someSite.com/camdsa");
        response = request.GetResponse() as HttpWebResponse;
    }
    catch (WebException webException)
    {
        Response.Redirect("ErrorPage.aspx?status="
+ webException.Status);
        return;
    } }

You can read about my solution at this link.

Upvotes: 1

risyasin
risyasin

Reputation: 1321

Alternative idea... You can check target url with server side language.

For PHP: with Curl you can get http status of url.

http_code is what you are looking for curl_getinfo ( $ch [, int $opt = 0 ] )

http://www.php.net/manual/en/function.curl-getinfo.php

Upvotes: 2

lonesomeday
lonesomeday

Reputation: 238115

Because of the same origin policy you cannot detect anything about the content of another window or frame -- including whether it actually even loaded -- if it is on a different domain name.

Upvotes: 6

Related Questions