Opening External Links in new window from AS3
You most probably know that there are issues opening links using the navigateToURL() method in Flex. The three most common problems / issues are:
- allowScriptAccess not set to all in the HTML Wrapper
- Window Mode set to transparent in IE
- Popup blockers blocking the new window - specially when target is _blank
Many people have tried solving this issue. My approach is simply a hack over FlexMerge's code. Which in turn is a hack on "Jason the SAJ"'s approach. Another approach I liked in detecting if popup blockers blocked your window is Andrew Trice's solution on InsideRIA.
Essentially, use JavaScript window.open method for Firefox, a little tweak on that for IE and use navigateToURL() for other browsers.
Continue reading for the code...
What I wanted to achieve was show help for a Flex application. Help is contextual, and we are using a separate knowledge base system to store help articles. Flex has to open a link and pass a keyword to look up for that help.
I created a class PopupWin that handles everything about opening a new window, ensuring it's not blocked. (methods are called statically, you can change them if you wish).
-
package
-
{
-
import flash.external.ExternalInterface;
-
import flash.net.URLRequest;
-
import flash.net.navigateToURL;
-
-
public class PopupWin
-
{
-
public static var baseURL:String = '';
-
private static var browserName:String = '';
-
-
public static function openWindow(url:String, target:String = '_blank', features:String=""):void
-
{
-
//Sets function name into a variable to be executed by ExternalInterface.
-
//Otherwise Flex will try to find a local function or value by that name.
-
var WINDOW_OPEN_FUNCTION:String = "window.open";
-
-
// Prefix baseURL if specified
-
if (PopupWin.baseURL != '')
-
{
-
url = PopupWin.baseURL + url;
-
}
-
var myURL:URLRequest = new URLRequest(url);
-
-
if (PopupWin.browserName == '')
-
{
-
PopupWin.browserName = PopupWin.getBrowserName();
-
}
-
-
switch (PopupWin.browserName)
-
{
-
//If browser is Firefox, use ExternalInterface to call out to browser
-
//and launch window via browser's window.open method.
-
case "Firefox":
-
ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, target, features);
-
break;
-
//If IE,
-
case "IE":
-
ExternalInterface.call("function setWMWindow() {window.open('" + url + "', '"+target+"', '"+features+"');}");
-
break;
-
// If Safari or Opera or any other
-
case "Safari":
-
case "Opera":
-
default:
-
navigateToURL(myURL, target);
-
break;
-
}
-
-
/*Alternate methodology...
-
var popSuccess:Boolean = ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, target, features);
-
if(popSuccess == false){
-
navigateToURL(myURL, target);
-
}*/
-
}
-
-
private static function getBrowserName():String
-
{
-
var browser:String;
-
-
//Uses external interface to reach out to browser and grab browser useragent info.
-
var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
-
-
//Determines brand of browser using a find index. If not found indexOf returns (-1).
-
if(browserAgent != null && browserAgent.indexOf("Firefox")>= 0) {
-
browser = "Firefox";
-
}
-
else if(browserAgent != null && browserAgent.indexOf("Safari")>= 0){
-
browser = "Safari";
-
}
-
else if(browserAgent != null && browserAgent.indexOf("MSIE")>= 0){
-
browser = "IE";
-
}
-
else if(browserAgent != null && browserAgent.indexOf("Opera")>= 0){
-
browser = "Opera";
-
}
-
else {
-
browser = "Undefined";
-
}
-
return (browser);
-
}
-
-
-
public static function showHelp(screen:String = 'home') :void
-
{
-
//var features:String = "menubar=yes,status=no,toolbar=yes,location=1,scrollbars=yes,resizable=1";
-
PopupWin.openWindow(screen, '_help');
-
}
-
}
-
}
Here's how I used it in my MXML Application:
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
-
applicationComplete="init()">
-
<mx:Script>
-
<![CDATA[
-
public function init():void
-
{
-
PopupWin.baseURL = 'http://localhost/kms/help/';
-
}
-
]]>
-
</mx:Script>
-
-
<mx:Button label="Home" click="PopupWin.showHelp('home')"/>
-
<mx:Button label="Introduction" click="PopupWin.showHelp('introduction')"/>
-
<mx:Button label="Using Help" click="PopupWin.showHelp('using')"/>
-
</mx:Application>
Note that the KMS URLs are specific to my implementation. You can use PopupWin.openWindow() directly.
Hope this helps! And thanks to FlexMerge and SAJ for their excellent work.
No related posts.

Very handy. Thanks a lot!
Mr. D
28 Nov 08 at 1:09 am
Hi!
Man, i´m trying to solve the popup blocker problem, but even doing exactly as you posted, still no success.
I have a HTTPService that is called when I click on a button, the result of this HTTPService points to a function where I´d like to popup the window.
Here is the result function for my HTTPService:
private function ExportarImagem(e:Object, tipo:Number):void{
if(tipo == 1){
ExternalInterface.call(“window.open”,
“../includes/graficoHTML.php?filename=”+ filename,
“_blank”, “”)
}else {
navigateToURL(
new URLRequest(“../includes/graficoPDF.php?filename=”
+ filename),
“_top”);
}
}
When tipo == 1, it should open a new window with the address on the function, but it doesn’t happen because it gets blocked.
I´m using and testing it on Firefox 3.0.1.
Can you help me out?
Arthur Gouveia
2 Dec 08 at 5:25 pm
[...] method works and on some navigationToURL(). While solving this problem, I came across with a good link which solves most of the window.open() issues on different [...]
Getting through Popup Blockers with window.open() in Flex | Xebia Blog
4 Jan 09 at 10:05 am
Hi!
Wondering that the Safari area is blank, that’s the part that it’s killing me. My popup window doesn’t open on Safari (Mac). It works if I disable the popup blocker but I guess that everybody has it enabled. If at least the browser tells me something it would be great but is just like clicking to nothing…
Do you have any idea how to solve this weird thing?
Thanks!
Ken
15 Jan 09 at 1:05 pm
@ken, try this… http://www.asserttrue.com/articles/2007/08/22/workaround-for-safari-blocking-flash-initiated-pop-ups
Dave
2 Feb 09 at 9:28 am
[...] sure all a href=”” target=”_blank” links are not blocked by browser popup blockers as you code to save headache at crunch [...]
Click Here site has been launched |
18 Feb 09 at 3:40 am
Thank you! I tried about three different popup scripts and nothing seemed to work. I was about to give up until I found and tried your script! Thanks.
Sandro
21 Feb 09 at 12:42 am
thanx, verry useful as3 script, tried many others but not working, yours rocks!
numediaweb
1 Jun 09 at 5:56 pm
Thank you but doesn’t working fine. IE8 is catching it!
Can anyone help me??
atasözleri
16 Mar 10 at 1:19 pm