Archive for May, 2008
Firefox Popup Blocker – Don’t get caught!
Don’t get caught by Firefox’s popup blocker! Have you ever had some windows blocked and some not? Here’s a way to help ensure that your navigateToURL works in FF (even though having a popup is typically not a good idea in the first place and should be used sparingly).
Firefox tries to help you out and block malicious popups. This has to do with opening a new window without a direct user-interaction (e.g. opening a popup on page load rather than opening a window when a user specifically clicks a button).
I worked on a project where I had to open a new window from within a Flex app. When the user clicked a button, it did some processing behind the scenes based on their input fields, assembled a query string, then made the request to open a new window. Sometimes it worked, sometimes it didn’t! It was a really hard issue to nail down, but after a lot of testing I finally concluded that it had to do with the amount of time taken before the request was sent out.
After you initiate a click event, there’s a set amount of time the browser has to respond to it. If a new window is opened within that time limit, then you can get past the popup blocker because this is an accepted behaviour. Otherwise, it assumes that the request is not potentially not valid. Firefox (and most browsers) blocks this type of action because of malicous applications that could open several windows at will without permission.
Depending on your machine and what you has running at the time, the amount of code you can execute before you choke is variable. I created a quick demo here that illustrates this. If your popup blocker is enabled, one button will open a blank page, the other should get blocked. Right-click to view source. Here’s a snippet from it:
{
if (event.target.id == "success_btn")
{
popupSuccess();
}
else
{
popupFail();
}
}
private function popupSuccess():void
{
openNewWindow();
}
private function popupFail():void
{
// do some sort of heavy processing before making the request
for (var i:int=0; i < 1000; i++)
{
var o:Object = new Object();
var num:Number = Math.log(Math.PI);
o.num = num;
}
openNewWindow();
}
private function openNewWindow():void
{
navigateToURL( new URLRequest(URL), '_blank');
}