Safari window.open() doesn't work

francesca picture francesca · Nov 1, 2016 · Viewed 12.6k times · Source

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?

Answer

Peter Tristianche picture Peter Tristianche · Nov 6, 2016

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