Entrepreneur Geek

Nirav Mehta on life, technology and future

Opening External Links in new window from AS3

with 9 comments

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

Actionscript:
  1. package
  2. {
  3.     import flash.external.ExternalInterface;
  4.     import flash.net.URLRequest;
  5.     import flash.net.navigateToURL;
  6.    
  7.     public class PopupWin
  8.     {
  9.         public static var baseURL:String = '';
  10.         private static var browserName:String = '';
  11.        
  12.         public static function openWindow(url:String, target:String = '_blank', features:String=""):void
  13.         { 
  14.             //Sets function name into a variable to be executed by ExternalInterface.
  15.             //Otherwise Flex will try to find a local function or value by that name.
  16.             var WINDOW_OPEN_FUNCTION:String = "window.open";
  17.            
  18.             // Prefix baseURL if specified
  19.             if (PopupWin.baseURL != '')
  20.             {
  21.                 url = PopupWin.baseURL + url;
  22.             }
  23.             var myURL:URLRequest = new URLRequest(url);
  24.            
  25.             if (PopupWin.browserName == '')
  26.             {
  27.                 PopupWin.browserName = PopupWin.getBrowserName();
  28.             }
  29.            
  30.             switch (PopupWin.browserName)
  31.             {
  32.                 //If browser is Firefox, use ExternalInterface to call out to browser
  33.                 //and launch window via browser's window.open method.            
  34.                 case "Firefox":
  35.                     ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, target, features);
  36.                    break;
  37.                 //If IE,
  38.                 case "IE":
  39.                     ExternalInterface.call("function setWMWindow() {window.open('" + url + "', '"+target+"', '"+features+"');}");
  40.                     break;
  41.                 // If Safari or Opera or any other
  42.                 case "Safari":
  43.                 case "Opera":
  44.                 default:
  45.                     navigateToURL(myURL, target);
  46.                     break;          
  47.             }
  48.            
  49.             /*Alternate methodology...
  50.                var popSuccess:Boolean = ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, target, features);
  51.             if(popSuccess == false){
  52.                 navigateToURL(myURL, target);
  53.             }*/           
  54.         }           
  55.  
  56.         private static function getBrowserName():String
  57.         {
  58.             var browser:String;         
  59.        
  60.             //Uses external interface to reach out to browser and grab browser useragent info.
  61.             var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");         
  62.        
  63.             //Determines brand of browser using a find index. If not found indexOf returns (-1).
  64.             if(browserAgent != null && browserAgent.indexOf("Firefox")>= 0) {
  65.                 browser = "Firefox";
  66.             }
  67.             else if(browserAgent != null && browserAgent.indexOf("Safari")>= 0){
  68.                 browser = "Safari";
  69.             }
  70.             else if(browserAgent != null && browserAgent.indexOf("MSIE")>= 0){
  71.                 browser = "IE";
  72.             }
  73.             else if(browserAgent != null && browserAgent.indexOf("Opera")>= 0){
  74.                 browser = "Opera";
  75.             }
  76.             else {
  77.                 browser = "Undefined";
  78.             }
  79.             return (browser);
  80.         }
  81.  
  82.        
  83.         public static function showHelp(screen:String = 'home') :void
  84.         {
  85.             //var features:String = "menubar=yes,status=no,toolbar=yes,location=1,scrollbars=yes,resizable=1";
  86.             PopupWin.openWindow(screen, '_help');
  87.         }
  88.     }
  89. }

Here's how I used it in my MXML Application:

Actionscript:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
  3.     applicationComplete="init()">
  4.     <mx:Script>
  5.         <![CDATA[
  6.         public function init():void
  7.         {
  8.             PopupWin.baseURL = 'http://localhost/kms/help/';
  9.         }         
  10.         ]]>
  11.     </mx:Script>
  12.    
  13.     <mx:Button label="Home" click="PopupWin.showHelp('home')"/>
  14.     <mx:Button label="Introduction" click="PopupWin.showHelp('introduction')"/>
  15.     <mx:Button label="Using Help" click="PopupWin.showHelp('using')"/>
  16. </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.

Written by Nirav

November 27th, 2008 at 11:58 am

 

9 Responses to 'Opening External Links in new window from AS3'

Subscribe to comments with RSS or TrackBack to 'Opening External Links in new window from AS3'.

  1. Very handy. Thanks a lot!

    Mr. D

    28 Nov 08 at 1:09 am

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

  3. [...] 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 [...]

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

  5. Dave

    2 Feb 09 at 9:28 am

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

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

  8. thanx, verry useful as3 script, tried many others but not working, yours rocks!

    numediaweb

    1 Jun 09 at 5:56 pm

  9. Thank you but doesn’t working fine. IE8 is catching it!

    Can anyone help me??

    atasözleri

    16 Mar 10 at 1:19 pm

Leave a Reply