Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.open(url, '_blank'); not working on iMac/Safari

I've build a web page that let's you select a page name from a drop down list and then transfers the browser to that page. The code that does the transfer is

if (url){     window.open(url, '_blank'); }  

where "url" is the page selected.

A console log just before the window.open line prints something like:

    executing: window.open(http://www.mywebsite.com/44/threats.html, '_blank') 

and then the browsers opens the page in a new tab.

This works fine on Windows 7 for all the browsers, including Safari.

On an iMac it works for Firefox but not for Safari.

Does anyone know why iMac/Safari won't do this?

like image 558
Steve Avatar asked Dec 20 '13 03:12

Steve


People also ask

Does window open work on Safari?

Safari blocks any window. open() function during an async call to provide "better" security. If you are using window. open() anyways inside an async call, Safari users will get a popup with the message, that a popup gets blocked.

Why am I getting Safari Cannot open page?

Sometimes Safari caches can cause strange browser behavior like displaying a “Can't open page” error message. Emptying the browser caches and site data can often remedy this issue.


2 Answers

Safari is blocking any call to window.open() which is made inside an async call.

The solution that I found to this problem is to call window.open before making an asnyc call and set the location when the promise resolves.

var windowReference = window.open();  myService.getUrl().then(function(url) {      windowReference.location = url; }); 
like image 177
Jeff Victorino Avatar answered Sep 19 '22 07:09

Jeff Victorino


To use window.open() in safari you must put it in an element's onclick event attribute.

For example: <button class='btn' onclick='window.open("https://www.google.com", "_blank");'>Open Google search</button>

like image 29
user2704238 Avatar answered Sep 21 '22 07:09

user2704238