如何检测window.location是否失败? [英] how do I detect if window.location failed?

查看:1183
本文介绍了如何检测window.location是否失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查对window.location的调用是否失败,因为给定的URL无效等?是否有一些事件可以设置在窗口对象或其他可以捕获它的对象上?

how do I check if a call to window.location failed because the given URL was invalid, etc? Is there some event I can set on the window object or on some other object that can catch this?

推荐答案

最后得到它使用解决方法工作,这不是我最初希望的通用解决方案:

Finally got it to work using a "workaround" that is not a generic solution as I originally hoped:

我正在使用我尝试打开的链接是自定义网址方案的事实(例如myxx:// localhost)在移动设备上,如果失败,我想要执行的操作是重定向到标准的appstore URL(特定于操作系统)。解决方法尝试打开自定义URL,如果失败,超时功能会在不久之后启动,并打开另一个URL:

I am using the fact that the link I am trying to open is a custom url scheme (e.g. myxx://localhost) on mobile, and if it fails, the action I want to perform is a redirection to a standard appstore URL (os-specific). The workaround tries to open the custom URL, and if it fails, the timeout function kicks in shortly after, and opens an alternative url:

setTimeout(function() { window.location=alternateUrl; }, 25);
window.location = customUrl;

缺点是当customURL失败时,标准的safari浏览器会显示一个消息框,该网站可以不能打开,但至少它仍然会将用户重定向到appstore。

The downside is that when the customURL fails, a standard safari browser shows a message box that the site could not be opened, but at least it still redirects the user to the appstore.

这篇关于如何检测window.location是否失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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