在页面加载时检测404错误 [英] detect 404 error on page load

查看:938
本文介绍了在页面加载时检测404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站中嵌入了谷歌地图,有时谷歌会回复404错误。我知道我应该知道这一点,但我记不起来了,我找不到它。此网站上其他404 iframe相关问题均未得到解答。

I am embedding a google map in my website and sometimes google responds with a 404 error. I know I should know this, but I can't remember and I cannot find it quickly. None of the other 404 iframe related questions on this site were ever answered.

如何检查javascript中是否有404响应?我计划在响应为404时调用location.reload()。

How do I check if there is a 404 response in javascript? I plan to call location.reload() when the response is 404.

<iframe src="https://mapsengine.google.com/map/embed?mid=zGLD-nMIJQAg.k0gTyNlOPz_Q" width="640" height="480"></iframe>

错误代码为:

GET https://khms0.googleapis.com/kh?v=159&hl=en&x=784456&y=1636828&z=22&token=122715 404 (Not Found)

我试过这个:没用。

var url = 'https://mapsengine.google.com/map/embed?mid=zGLD-nMIJQAg.k0gTyNlOPz_Q';
if (url.statusCode === '404'){
    window.location.reload();
}


推荐答案

var url = 'https://mapsengine.google.com/map/embed?mid=zGLD-nMIJQAg.k0gTyNlOPz_Q';
function UrlExists(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    if (http.status != 404)
        //  do something
    else
        window.location.reload();
}

使用此功能并将URL作为参数传递。如果状态不是404那么什么都不会发生,或者你可以做其他事情,它会根据你的要求刷新页面。

Use this function and pass the URL as Parameter.if the status is not 404 then nothing will happen or you can do something else it will refresh the page as per your requirement.

这篇关于在页面加载时检测404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆