Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript window.open in safari

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)

like image 973
theMothaShip Avatar asked Mar 26 '12 21:03

theMothaShip


People also ask

Does window open work for Safari?

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

What is window open JavaScript?

The Javascript window. open() method is used to open a new tab or window with the specified URL and name. It supports various parameters that can be used to specify the type and URL location of the window to be opened. Syntax: window.open(url, windowName, windowFeatures)

How do I open a JavaScript browser?

In JavaScript, window.open() method is used to open a new browser window or a new tab depending on the browser setting and the parameter values.


2 Answers

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.

like image 180
Lucas Serafim Avatar answered Sep 28 '22 19:09

Lucas Serafim


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!

like image 34
Scorpion-Prince Avatar answered Sep 28 '22 18:09

Scorpion-Prince