Safari window.open() 不起作用 [英] Safari window.open() doesn't work

查看:53
本文介绍了Safari window.open() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在新窗口中打开外部链接.我处理视图中编辑按钮的点击:

I need to open external link in a new window. I handle click on edit button in a view:

module.exports = utils.Backbone.View.extend({
    events: {
        "click #edit": "onEditClicked"
    },

    "onEditClicked": () => PubSub.publish("EDITOR_REQUESTED");
});

然后我检查用户是否登录.如果是 - 我发送通知OPEN_EDITOR",并希望使用外部链接打开一个新窗口.

Then I check if the user is logged in. If yes - I send notification "OPEN_EDITOR" and expect a new window to be open with the external link.

TextEditorController.prototype.handleMessages = function () {

    PubSub.subscribe("OPEN_EDITOR", () => {
        var editor = window.open(this.$service.getEditorURL());
    });
});

但是在 Safari 中新窗口似乎被阻止了?我的情况有解决方法吗?

But in Safari new window seems to be blocked? Is there workaround in my case?

推荐答案

原因是 Safari 内置的弹出窗口拦截器.

The reason of it is Safari's built-in pop-up blockers.

唯一允许在 Safari 中打开新窗口的 javascript - 是直接附加到用户事件的 javascript.就您而言,您稍后会调用 window.open.

The only javascript that is allowed to open a new window in Safari - is javascript directly attached to user's event. In your case, you're calling window.open later.

这里的解决方法可以是:

The workaround here can be:

  • 在 onEditClicked 方法中创建一个没有 URL 的窗口

  • to create a window without a URL in onEditClicked method

safariWindow = window.open();

在 handleMessages 函数中更改该窗口的 URL

change that window's URL in handleMessages function

safariWindow.location.href = newUrl

这篇关于Safari window.open() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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