Quicktip: Avoiding popup blockers in AS3

Adobe/Macromedia did change a couple of things as the release of ActionScript 3 became a reality.
For once, it's not as easy as it once was to open an external browser window. There are many solutions to this floating around, many of them involve using the "wmode" parameter in the HTML embed code for the swf. In my opinion this is not a good idea cos it will bring a bunch of new problems with it, like handling textfields and so on. I've written a possible solution for this problem which works using standard embed code in the HTML. It simply tries to use the ExternalInterface, if that fails, it falls back to navigateToURL. Hey, remember to import ExternalInterface, URLRequest and NavigateToURL in order for it to compile properly.

private function openURL(url:String, window:String = "_blank") : void {
var WINDOW_OPEN_FUNCTION:String = "window.open";
if(!ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window)){
navigateToURL(new URLRequest(url),window);
}
}

5 comments:

Anonymous said...

But how do you keep from having 2 windows open if you have a pop up blocker off?

Unknown said...

Due to the if(ExternalInterface....) check, you will only have one windows opened. If the first one succeeds the backup will never get a call

Anonymous said...

Interesting post you got here. I'd like to read something more about this topic. The only thing that blog needs is a few photos of some devices.
David Stepman
Mobile phone blocker

Jay said...

window.open will still generate pop ups :(
There's a solution here which works across all browsers and successfully bypasses pop up blockers with actionscript 3 navigateToURL

http://www.codebasement.com/2010/01/18/actionscript-3pop-up-blocker-ie-and-navigatetourl-wont-open/

Hope this helps

Anonymous said...

Just popping in to say nice site.