admin管理员组

文章数量:1201372

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?

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?

Share Improve this question asked Nov 1, 2016 at 14:26 francescafrancesca 5873 gold badges10 silver badges21 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

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

If you want to open a popup window in the Safari browser, you need to follow the below code. Open popup window before getting URL from the backend.

const width = 500;
const height = 600;
const top = window.innerHeight / 2 - height / 2;
const left = window.innerWidth / 2 - width / 2;
const params = `modal=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${width}, height=${height}, top=${top}, left=${left}`;
if (oauthPopup === null || oauthPopup.closed) {
  console.log(oauthPopup);
  oauthPopup = window.open("", "", params);
}

After getting a URL from the backend, change the location of the popup window.

await request()
  .get(`/sources/authorizationEndpoint`, {
    headers: {
      authorizer: token,
    },
  })
  .then((response) => {
    const url = response.data.oauth_url;
    oauthPopup.location = url;
    oauthPopup.focus();       
  })
  .catch((err) => {
    oauthPopup.close();
  });

本文标签: javascriptSafari windowopen() doesn39t workStack Overflow