javascript window.open in safari

76,288

Solution 1

I don't think there is a way to open a new window in mobile safari other than from a button click. Refer to this StackOverflow Question which is similar. I'm not sure if it will work, but you can look at triggering a button click programatically using jquery's trigger() function.

You might also want to look at options of showing a dialog within your own page, maybe using tools like jquery ui.

HTH!

Solution 2

The safari has a pop-up blocker silencer not show when a link is blocked.

To check if the pop-up blocker is active, go on safari settings > security > something like blocking pop-ups.

To cross it in a simple way, since I can not open a new window, I display an alert showing pop-up blocked.

In my case, I use select inputs to open external links:

HTML

<select id="retailer" class="windowOpen retailer-submenu">
    <option value="null">Select one</option>
    <option value="http://amazon.com">Amazon</option>
    <option value="http://ebay.com">eBay</option>
</select>

Javascript

<script type='text/javascript'>
    $('select.windowOpen').change(function(){
        var url = $(this).val();

        var open = window.open(url);
        if (open == null || typeof(open)=='undefined')
            alert("Turn off your pop-up blocker!\n\nWe try to open the following url:\n"+url);
    });
</script>

The code to check if a pop-up is blocked is just this:

var open = window.open('http://google.com');
if (open == null || typeof(open)=='undefined')
    alert("Turn off your pop-up blocker!");

PS: the jquery trigger did not work with me.

Share:
76,288
theMothaShip
Author by

theMothaShip

Updated on October 11, 2021

Comments

  • theMothaShip
    theMothaShip over 2 years

    I've run into an issue with opening a new window in safari (both ipad and desktop versions) that revolves around the popup blocker. Basically I've found that if window.open isn't called from a click event, safari will block the popup.

    The event that is calling window.open is currently onchanged from a list box.

    Is there any way other than switching which event we handle to trick safari into allowing a popup in this scenario? (the onchanged event)

  • theMothaShip
    theMothaShip about 12 years
    Unfortunately, when you try to trigger another elements "click" event, safari also blocks those as popups. The only way to get a popup, is to have a user generated click event on a control, which I would be fine with if when you selected an item from the dropdown on an Ipad, it counted as a "click", however it doesn't fire the click event... :( Time for some degradation code!
  • theMothaShip
    theMothaShip almost 12 years
    I ended up going with a graceful degradation approach which allowed the user to still use the select to pick where they wanted to go and then a button to actually perform the navigation... That way I'm not popping up any obtrusive messages
  • Jon z
    Jon z about 10 years
    Yeah basically any decent browser will not allow window.open except when the execution can be traced to a user-initiated event.
  • cw24
    cw24 over 9 years
    In my opinion the real problem seems to be the built-in silent popup blocker.
  • scunliffe
    scunliffe about 9 years
    Sadly this breaks even when the user generated the click... which triggers an asynchronous action (say pulling data from an IndexedDB)... where the callback tries to open the window :-(
  • samneric
    samneric over 7 years
    Thanks. I ended up displaying a pop-up in safari browsers with an Open button that ensured the window.open command was performed in a user-initiated stack.
  • Miguel
    Miguel over 2 years
    I believe the safari is "judging" user interaction wrong. Chromes does it better. The user initiated a click in the picker, if its a change event it should count anyway... This breaks so many things