在 IE9 中的 onunload 事件处理程序中调用时 window.open 不再起作用

发布于 2024-10-28 01:26:15 字数 1076 浏览 0 评论 0原文

在我的 Web 应用程序中,我使用自定义 url 协议启动桌面应用程序。例如:“fakeproto://”将在您的桌面上启动“fakeproto.exe”。如果您不知道我在说什么,请阅读以下内容:将应用程序注册到 URL 协议

不过,我需要一种方法来确保在尝试启动之前“fakeproto.exe”已安装在用户的 PC 上。这有点像黑客,但我让它适用于所有主要浏览器。 IE 提出的问题最多,并且有独特的实现。

在Javascript中,我首先尝试加载自定义url协议:

window.open('fakeproto://', '_self');

在此之前,我实际上定义了以下onunload事件处理程序:

window.onunload = function()
{
     window.open('help.php', '_self');
}

因此,如果桌面无法识别自定义url协议,IE将简单地离开当前页面并转到“网页无法显示”。在这种情况下,onunload 事件处理程序将触发并打开 help.php。

这在 IE7 和 IE7 中效果很好。 IE8,但是升级到IE9后,这个就不再起作用了?它转到“网页无法显示”而不是help.php。

使用调试器, onunload 事件处理程序正在触发并且代码正在正确执行,但由于某种原因 window.open 调用不起作用???我还禁用了弹出窗口阻止程序,以确保不是这样。运气不好。

有人有什么想法吗?有人听说过 IE9 对 window.open 更加严格吗?有人知道原始问题的任何替代解决方案吗?

顺便说一句,如果我在新窗口中打开 help.php,我就可以让它工作。

window.onunload = function()
{
     window.open('help.php', '_blank');
}

但这仅在禁用任何弹出窗口阻止程序时才有效。我想避免使用这个解决方案。

In my web application, I launch a desktop application using a custom url protocol. For example: "fakeproto://" will launch "fakeproto.exe" on your desktop. If you don't know what I'm talking about, read this: Registering an Application to a URL Protocol

I needed a way, though, to make sure "fakeproto.exe" was installed on the user's PC before attempting to launch. It's a bit of a hack, but I got it to work for all the major browsers. IE presented the most problems and had a unique implementation.

In Javascript, I would first try to load the custom url protocol:

window.open('fakeproto://', '_self');

Before this, I actually defined the following onunload event handler:

window.onunload = function()
{
     window.open('help.php', '_self');
}

So if the desktop didn't recognize the custom url protocol, IE would simply leave the current page and go to "webpage cannot be displayed". In this event, the onunload event handler would fire and open help.php instead.

This works great in IE7 & IE8, but once I upgraded to IE9, this no longer works? It goes to "webpage cannot be displayed" instead of help.php.

Using a debugger, the onunload event handler is firing and the code is being executed correctly, but for some reason the window.open call isn't working??? I disabled the pop-up blocker as well, to make sure it wasn't that. No luck.

Anyone have any ideas? Anyone hear of IE9 being more strict with window.open? Anyone know of any alternative solutions to the original problem?

BTW, I can get it to work if I open help.php to a new window.

window.onunload = function()
{
     window.open('help.php', '_blank');
}

But this only works if any pop-up blockers are disabled. I would like to avoid using this solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-11-04 01:26:15

是的,IE9 会阻止 onunload 处理程序中的导航。

Yes, IE9 blocks navigations in the onunload handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文