1

i am trying to open url when user clicks on a button in plugin view. But web addin is throwing error.

My code to open url :

let a = document.createElement("a");
a.setAttribute('target', '_blank');
a.setAttribute("style", "display: none");
document.body.appendChild(a);
a.href = finalUrl;
a.click();
document.body.removeChild(a);

Error message :

enter image description here

App is rejected from store because of this error. How can i overcome this error?

I don't want to use Dialogue API which doesnot open url in the browser.

Rajeev
  • 4,762
  • 8
  • 41
  • 63

1 Answers1

1

The general rule is opening a window will always be blocked when it isn't the direct result of a user action. For more information, see this SO question: Avoid browser popup blockers.

You're triggering a popup because your attempting to emulate a click(). Since this is not a direct user action, this action will get flagged by all mainstream browsers.

You will need to present the user with a clickable element (link, button, etc) that opens a new window.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • Here I don’t have url at the time of rendering. When user clicks on button I generate amazon s3 url dynamically and opens it. What is the work around to handle these kind of scenarios? – Rajeev Oct 04 '17 at 16:05
  • You should generate the `URI` and call `window.open` within that button's click event handler. Where you run afoul of the blocker is when you place that call deeper within the call stack. The alternative would be to have the user click the button and then present them with another button when the URI is ready. – Marc LaFleur Oct 04 '17 at 16:38