Safari blocking popups from Flash – fix

I dont understand why Apple can make the best hardware in the world, and then proceed to create the worst software to put on it. Safari (like iTunes) is full of bugs, one of the most obivously yet most poorly documented is the problem that causes Safari to block pop-up windows from Flash even after a user has clicked to intiate the popup.

Here’s the fix:

ActionScript:

import flash.net.*;

...

mybutton.addEventListener(MouseEvent.CLICK, openPopup);

function openPopup(event:Event) {
	var jsPopFunc:String = "popupWin";
	var _winURL:String = "http://www.rivastakeout.com/staging/stake-your-claim/form.php";
	var _winTarget:String = "_self";
	var _winWidth:Integer = 600;
	var _winHeight:Integer = 400;
	var _winName:String = "popup";
	var _winParams:String = "location=0,status=0,scrollbars=1";;
	
	var result:Boolean;	
	if (ExternalInterface.available) {
		result=Boolean(ExternalInterface.call(jsPopFunc , _winURL, _winTarget, _winWidth, _winHeight, _winName, _winParams));
	}
	if (!result) {
		var request:URLRequest=new URLRequest(_winURL);
		try {
			navigateToURL(request, _popupWin);
		} catch (e:Error) {
			trace("Error occurred!");
		}
	}	
}

JavaScript:

  function popupWin(winURL, width, height, name, params){
	var left = Math.floor((screen.width - width)/2);
	var top = Math.floor((screen.height - height)/2);
	var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;

	if(!name){
		name = "newWin";
	}
	if(!params){
		params = "location=0,status=0,scrollbars=0";
	}
	if (params){
		winParms += "," + params;
	}

	var newWin = window.open(winURL, name, winParms);
	if (newWin) {
		return true;
	}
	else{
		return false;
	}
  }

Further reading:
http://www.ultrashock.com/forums/actionscript/popups-from-flash-solution-92036.html

Leave a Reply

Your email address will not be published. Required fields are marked *