127

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?

InSync
  • 4,851
  • 4
  • 8
  • 30
Steve
  • 4,534
  • 9
  • 52
  • 110

11 Answers11

282

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;
});
Jeff Victorino
  • 2,891
  • 1
  • 11
  • 7
48

Using setTimeout

Wrapping your window.open(url, '_blank') line of code in the async function with a setTimeout works as well,

setTimeout(() => {
    window.open(url, '_blank');
})

setTimeout code runs on the main thread, instead of the asynchronous one. Tested in Chrome and Safari.

Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
24

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>

user2704238
  • 495
  • 4
  • 7
22

You can't rely on window.open because browsers may have different policies. I had the same issue and I used the code below instead.

let a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = <your_url>;
a.download = <your_fileName>;
a.click();
document.body.removeChild(a);
David Buck
  • 3,752
  • 35
  • 31
  • 35
Abreham
  • 425
  • 1
  • 4
  • 14
14

window.location.assign(url) this fixs the window.open(url) issue in ios devices

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
sama vamsi
  • 197
  • 1
  • 4
  • 8
    This causes the `url` to be opened in the current tab, not in a new tab (as specified in the question). – Jon Schneider Jan 17 '19 at 16:19
  • Yup. This worked for me after fidgeting around a bit. I was trying to capture onScroll in an iPhone and it wasn't working though it was working on Safari in general. Thanks man. – anotherDev Feb 05 '20 at 06:13
  • @JonSchneider It causes to open in the current tab because it is assigned to `window`. Besides that, assigning the `url` works better in the senario where your `url` is a `String` type – Nebulosar Jun 25 '20 at 12:43
  • Its working For ME. I don't need a popup window. Just need to open url. Thank you so much... – Bala Murugan Jul 14 '20 at 09:47
  • I wanna add a comment that this also works when you would like to download some images or documents from a certain url. – Ezrqn Kemboi Dec 23 '20 at 07:19
13

Taken from the accepted answers comment by Steve on Dec 20, 2013:

Actually, there's a very easy way to do it: just click off "Block popup windows" in the iMac/Safari browser and it does what I want.

To clarify, when running Safari on Mac OS X El Capitan:

  1. Safari -> Preferences
  2. Security -> Uncheck 'Block pop-up windows'
oguz ismail
  • 1
  • 16
  • 47
  • 69
jnrcorp
  • 1,905
  • 1
  • 18
  • 25
  • 55
    but you cannot block customers' popup windows – xiaoyu2er May 18 '17 at 00:53
  • 1
    @madd I think the point is that you need to go back to your designer and figure out how to solve the need without popping open a new window via JS or use a traditional `href`. The reason this answer is here is to alert people that browsers are making it more difficult to open popups. – jnrcorp Jun 15 '21 at 12:25
  • 1
    Downside: On IPads there does not seem to be a way to generally accept popups for a certain website so you have to confirm that you really want to open the popup every single time. – René Jul 15 '21 at 07:34
7

Open link in a new tab, programatically with JavaScript for: safari, mobile safari and other browsers:

const link = 'https://google.com';

const a = document.createElement("a");
a.setAttribute('href', link);
a.setAttribute('target', '_blank');
a.click();
Sergii
  • 1,643
  • 1
  • 14
  • 9
  • 1
    This trick doesn't work in safari ios. – liberborn Jul 27 '22 at 11:48
  • @liberborn, just checked on safari ios (iPhone) and it works. You need to add something like this: const onClickHandler = () => { const link = 'https://google.com'; const a = document.createElement("a"); a.setAttribute('href', link); a.setAttribute('target', '_blank'); a.click(); } – Sergii Apr 24 '23 at 12:59
1

There's a setting in Safari under "Tabs" that labeled Open pages in tabs instead of windows: with a drop down with a few options. I'm thinking yours may be set to Always. Bottom line is you can't rely on a browser opening a new window.

Bill Criswell
  • 32,161
  • 7
  • 75
  • 66
  • 1
    I don't think this is it. Safari doesn't open the site anywhere. It's like I didn't execute the line. – Steve Dec 20 '13 at 06:49
1

It will not work simply because safari has no support for that.

Check MDN Docs for compatibility - https://developer.mozilla.org/en-US/docs/Web/API/Window/open#browser_compatibility

0

This should work: window.location.assign(url); Usually it is important to save the state, before leaving the page, so have this in mind as well.

lyuboe
  • 151
  • 1
  • 6
-14

The correct syntax is window.open(URL,WindowTitle,'_blank') All the arguments in the open must be strings. They are not mandatory, and window can be dropped. So just newWin=open() works as well, if you plan to populate newWin.document by yourself. BUT you MUST use all the three arguments, and the third one set to '_blank' for opening a new true window and not a tab.

Guido
  • 7
  • Incorrect, please, read the documentation first: https://developer.mozilla.org/en-US/docs/Web/API/Window/open – elpezganzo Mar 17 '21 at 20:46