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;
});
Answer from Jeff Victorino on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Window › open
Window: open() method - Web APIs | MDN
The open() method of the Window interface loads a specified resource into a new or existing browsing context (that is, a tab, a window, or an iframe) under a specified name.
🌐
Zkoss
forum.zkoss.org › question › 111445 › problem-script-windowopen-on-iphone-and-ipad
Problem script window.open on iphone and ipad - ZK Forum
https://stackoverflow.com/questions/5649962/how-can-i-indicate-that-a-popup-has-been-blocked-by-safari · The workaround is to render a link the user has to click in order to open the tab/window in a _blank target. ... Sorry for no better alternative...
🌐
SitePoint
sitepoint.com › javascript
Window.open Not woking in safari(its very urgent)
December 26, 2007 - Hi: 1 I am using javascript window.open() method to open new window, it works well inIE,Firefox but not working in safari. 2 I have problem on firefox, image control never displays image in firefox.I have mentioned the…
🌐
DSL Reports
dslreports.com › forums › software and operating systems › web developers › js window.open works in ff but not in safari
JS window.open works in FF but not in Safari - Web Developers | DSLReports Forums
Have you looked in to the alternative, an overlay window? Something like GreyBox is not too difficult to set up with an iframe displaying whatever page you'd like (check out that first example link). ... This works for me in all versions of Safari with the pop-up blocker enabled. I don't even remember where I stole it from, but I don't think it does anything fancy: <SCRIPT LANGUAGE='JavaScript'> var newwindow; function poptastic(url) { newwindow=window.open(url,'name','height=250,width=450,scrollbars=0,toolbar=0,menubar=0,location=0,resizable=1,status=1'); if (window.focus) {newwindow.focus()} } </SCRIPT> What's safari's debug console have to say about the code you're using?
🌐
PlayCanvas
forum.playcanvas.com › help & support
[SOLVED] Window.open does not work on ios safari - Help & Support - PlayCanvas Discussion
July 28, 2020 - we are creating a game and need to open some windows on button clicks, this does not seem to work on ios safari due to pop-up blocking. we already looked at other projects and are using the same code as the flappy bi…
Find elsewhere
🌐
Apple Community
discussions.apple.com › thread › 253020466
[Safari 14]window.open with parameters wi… - Apple Community
And if we set "Open pages in tabs instead of windows" to Always, it will open in new tab. Safari with old versions and other browsers are also opening page in new tab by default for this case.
🌐
Webmaster World
webmasterworld.com › html › 3031854.htm
alternative to window.open() - HTML forum at WebmasterWorld - WebmasterWorld
<a onclick="window.open('webpage.htm' ,'Max', 'toolbar=no, width=400, height=275, left=100, top=100, screenX=200, screenY=150, status=yes, scrollbars=yes, resize=yes'); return false" href="#"> This code is currently being stopped by pop-up blockers. There is some concern that since most of our customers are fairly computer illiterate that they may not realize that the link is working it's just being blocked. Does anyone know of an alternative way of opening a link into a resized new window without using window.open?
Top answer
1 of 2
2

If you simply use the open command, Safari will open your URL in a new tab or new window, depending on the end user's preferences. You can test different experiences in Safari by opening Settings > Tabs and playing with the options for opening pages in tabs instead of windows.

AppleScript and osascript are your only options to control whether Safari opens URLs in new tabs or new windows, and Apple still very much supports them. (I wouldn't put much stock in someone's speculation that AppleScript is going away without solid supporting evidence, which can only come from Apple.) This isn't something you can do with bash alone.

This will open a new tab in the front window of Safari with your URL or open a new Safari window if one doesn't exist:

#!/bin/zsh

/usr/bin/osascript -e 'tell application "Safari" to get front document'

if [[ "$?" -eq 0 ]]; then
    # open URL in new tab of a Safari window if one exists
    /usr/bin/osascript -e 'tell application "Safari" to make new tab with properties {URL: "https://www.apple.com"} at front window'
else
    # open URL in new Safari window
    /usr/bin/osascript -e 'tell application "Safari" to make new document with properties {URL: "https://www.apple.com"}'
fi

You can use the second osascript command by itself to always open your URL in a new Safari window.

You don't want to use open -n because that literally opens a new Safari application rather than using the one that's currently running. When you do this, you'll notice two instances of Safari running in your dock or app switcher.

2 of 2
0

The simplest solution would be to change Safari's settings for tabs to open pages in windows. See the below image taken from Ventura 13.3.1 (a).

However, since you asked for bash, I will offer the following script, which does not use the osascript command. When necessary, this script temporarily changes the setting circled in red, so a new window is created instead of a new tab.

Note: In order for the defaults command to work properly when used in a Terminal window, the Terminal application will need to be given Full Disk Access. See Privacy & Security in the System Setting application (formally known as the System Preferences application).

#!/bin/bash
unset -v TabCreationPolicy ExitStatus
if ! TabCreationPolicy="$(defaults read -app Safari TabCreationPolicy 2>/dev/null)"; then
  TabCreationPolicy=1
fi
if [[ $TabCreationPolicy != 0 ]]; then
  defaults write -app Safari TabCreationPolicy 0
fi
open -a Safari "${1:-https://www.google.com}"
ExitStatus=$?
if [[ $TabCreationPolicy != 0 ]]; then
  defaults write -app Safari TabCreationPolicy "$TabCreationPolicy"
fi
exit $ExitStatus

The relationship between the selections available for the combo box shown in the above image and the value of the TabCreationPolicy key is given in the table below.

Open pages in tabs instead of windows Value of TabCreationPolicy key
Never 0
Automatically 1 or nonexistent
Always 2
🌐
GitHub
github.com › seleniumhq › selenium-google-code-issue-archive › issues › 3693
[SafariDriver] New windows cannot be opened via javascript (i.e. window.open) · Issue #3693 · SeleniumHQ/selenium-google-code-issue-archive
September 2, 2015 - That means none of the following will work: ((JavascriptExecutor) driver).executeScript("window.open('')"); // Assuming webElement is like: <a href="javascript:window.open()"> webElement.click(); // Assuming webElement has an onclick event handler like: // element.addEventListener('click', function() { // window.open(); // }, true); webElement.click(); -------------------------------- This limitation appears to be by design: http://developer.apple.com/library/safari/#documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
Author   lukeis
🌐
Jens Arps
jensarps.de › home › browser struggle › safari and window.open
Safari and window.open | Jens Arps
October 28, 2015 - So far for the theory the window.open call had to be in the same function body… · One step further: using dojo’s pub/sub system. So I connected a button to an anonymous function that published a topic, and subscribed to that topic, in different manners. Still Safari opened a new window every time, as ordered.
🌐
Experts Exchange
experts-exchange.com › questions › 28165427 › Open-Window-with-Javascript-other-method-in-Safari-IOS-Pop-up-issue.html
Solved: Open Window with Javascript (?other method) in Safari IOS. Pop-up issue | Experts Exchange
November 17, 2012 - You have to enable pop-ups in Safari, and you still get a warning even if pop-ups are allowed. I am new to IOS web development and I'll have to look into something like Phonegap or other development kit, but for the time being seems like this is a simple problem, and the rest of the site actually works pretty well. I suppose that if I want to make an app I will have to get a bit more involved. <select onchange="openHTML5('Genitourinary_name')" name="Genitourinary_name" id="Genitourinary_name"> <option value="none">- Select Case File -</option> <option value="/cases/Genitourinary/Crohn Disease/index.html">Crohn Disease</option> </select>
🌐
PlayCanvas
forum.playcanvas.com › help & support
[SOLVED] Window.open() doesn't work in Safari in iPhone - Help & Support - PlayCanvas Discussion
September 23, 2022 - window.open() doesn’t work in safari in iPhone. “Doesn’t work” means it does not open a window when it is called. Nothing happens. It works in Safari in iPad. It works in Safari in desktop. It works in chrome in anyw…
🌐
Experts Exchange
experts-exchange.com › questions › 28579365 › Jquery-or-Javascript-alternative-to-window-open-to-get-a-different-target.html
Solved: Jquery or Javascript alternative to window.open to get a different target | Experts Exchange
December 11, 2014 - jQuery will emulate the click in so far as any click events/functions attached to the link will be fired It cannot simulate a person actually clicking the link. There is no reason a simple window.open shouldn't work
🌐
Reddit
reddit.com › r/macos › for the love of god, how can i open a new safari window quickly?
r/MacOS on Reddit: For the love of God, how can I open a new Safari window quickly?
July 30, 2021 -

Every time I want a new Safari window, I have to move mouse to dock and right click and select new window.

Is there any keyboard-centric way to open a new window in current desktop?

I searched through Shortcut settings but there was nothing there.

All I want is a new Safari window in my current desktop, as quickly as possible with a few keystrokes. Spotlight works wonderfully to open apps and search things quickly, but it can't open a new window.

EDIT: I don't mean when I'm already in Safari. I know Command-N opens a new window when you're in Safari.

🌐
Stack Overflow
stackoverflow.com › questions › 58726457 › window-open-alternative
Window.open alternative
Actually my requirement is, In my website i have integrated third party website API. SO when i will login in my website it should be automatic login in that party website as well l. for that auto login i have used window.open and pass the third party website url