in Technology

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).

[AS]
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’);
}
}
}
[/AS]

Here’s how I used it in my MXML Application:
[AS]







[/AS]

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.

Write a Comment

Comment

  1. 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?

  2. 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!

Webmentions

  • Click Here site has been launched | June 27, 2010

    […] sure all a href=”” target=”_blank” links are not blocked by browser popup blockers as you code to save headache at crunch […]

  • Getting through Popup Blockers with window.open() in Flex | Xebia Blog June 27, 2010

    […] 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 […]