Safari window.open() doesn't work

痴心易碎 提交于 2020-05-27 06:37:05

问题


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");
});

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());
    });
});

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


回答1:


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

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:

  • to create a window without a URL in onEditClicked method

    safariWindow = window.open();

  • change that window's URL in handleMessages function

    safariWindow.location.href = newUrl



来源:https://stackoverflow.com/questions/40362093/safari-window-open-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!