开发者

404 detection in JavaScript

开发者 https://www.devze.com 2023-02-01 13:32 出处:网络
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:

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 c开发者_开发知识库ase 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?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消