Adobe AS3 Tour / Back from Munich
Post by Phil / April 8th, 2008

Problems using navigateToURL

Hi there... we've been busy these past couple of days so there wasn't much time for new posts - sorry. But here is a little update.
While developing a flash website (AS3 & SWFObject2.0) that heavily depended on opening URLs in a new browser window we came across the popup blocker problem that the use of navigateToURL causes. When trying to open a new window firefox'/IE's popup blocker will block the window and display its warning. After googling for some time we came across some neat workarounds:

1) How to prevent pop-up blocking in Firefox by Sergey Kovalyov

2) The Nightmare that is "_blank": Part II (resolved???) by The Saj

putting these two approaches into one AS3 util-class, this is what we came up with:

  1. package com.apdevblog.utils
  2. {
  3.   import flash.external.ExternalInterface;
  4.   import flash.net.URLRequest;
  5.   import flash.net.navigateToURL
  6.   /**
  7.    * Collection of URL util functions.
  8.    *
  9.    * ActionScript 3.0 / Flash 9
  10.    *
  11.    * @package    com.apdevblog.utils
  12.    * @author     Philipp Kyeck / phil@apdevblog.com
  13.    * @version    SVN: $Id: URLUtils.as 17 2008-04-08 09:13:30Z phil $
  14.    *
  15.    * based on script by
  16.    * @author Sergey Kovalyov
  17.    * @see http://skovalyov.blogspot.com/2007/01/how-to-prevent-pop-up-blocking-in.html
  18.    *
  19.    * and based on script by
  20.    * @author Jason the Saj
  21.    * @see http://thesaj.wordpress.com/2008/02/12/the-nightmare-that-is-_blank-part-ii-help
  22.    */
  23.   public class URLUtils    
  24.   {
  25.     protected static const WINDOW_OPEN_FUNCTION:String = "window.open";
  26.    
  27.     /**
  28.      * Open a new browser window and prevent browser from blocking it.
  29.      *
  30.      * @param url        url to be opened
  31.      * @param window     window target
  32.      * @param features   additional features for window.open function
  33.      */
  34.     public static function openWindow(url:String, window:String = "_blank", features:String = ""):void
  35.     {
  36.       var browserName:String = getBrowserName();
  37.      
  38.       if(getBrowserName() == "Firefox")
  39.       {
  40.         ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
  41.       }
  42.       //If IE,
  43.       else if(browserName == "IE")
  44.       {
  45.         ExternalInterface.call("function setWMWindow() {window.open('" + url + "');}");
  46.       }
  47.       //If Safari
  48.       else if(browserName == "Safari")
  49.       {              
  50.         navigateToURL(new URLRequest(url), window);
  51.       }
  52.       //If Opera
  53.       else if(browserName == "Opera")
  54.       {    
  55.         navigateToURL(new URLRequest(url), window);
  56.       }
  57.       //Otherwise, use Flash's native 'navigateToURL()' function to pop-window.
  58.       //This is necessary because Safari 3 no longer works with the above ExternalInterface work-a-round.
  59.       else
  60.       {
  61.         navigateToURL(new URLRequest(url), window);
  62.       }
  63.     }
  64.    
  65.     /**
  66.      * return current browser name.
  67.      */
  68.     private static function getBrowserName():String
  69.     {
  70.       var browser:String;
  71.            
  72.       //Uses external interface to reach out to browser and grab browser useragent info.
  73.       var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
  74.        
  75.       //Determines brand of browser using a find index. If not found indexOf returns (-1).
  76.       if(browserAgent != null && browserAgent.indexOf("Firefox") >= 0)
  77.       {
  78.         browser = "Firefox";
  79.       }
  80.       else if(browserAgent != null && browserAgent.indexOf("Safari") >= 0)
  81.       {
  82.         browser = "Safari";
  83.       }            
  84.       else if(browserAgent != null && browserAgent.indexOf("MSIE") >= 0)
  85.       {
  86.         browser = "IE";
  87.       }        
  88.       else if(browserAgent != null && browserAgent.indexOf("Opera") >= 0)
  89.       {
  90.         browser = "Opera";
  91.       }
  92.       else
  93.       {
  94.         browser = "Undefined";
  95.       }
  96.       return (browser);
  97.     }
  98.   }
  99. }
  100.  

You also have to set the wmode inside your containing html file to "opaque" and the allowScriptAccess to "always".

  1. // emdbedding swf w/ swfobject
  2. var width="400";
  3. var height="150";
  4. var flashVersion = "9.0.0";
  5. var movie = "navigatetourl.swf";
  6. var movieName = "flashMovie";
  7. var bgColor = "#000000";
  8. var express = "expressInstall.swf";
  9. var replaceDiv = "flashcontent";
  10.  
  11. var flashvars = {};
  12.  
  13. var params = {};
  14. params.wmode = "opaque";
  15. params.allowScriptAccess = "always";
  16.  
  17. var attributes = {};
  18. attributes.id = "myFlashMovie";
  19.  
  20. swfobject.embedSWF(movie, replaceDiv, width, height, flashVersion,
  21.                express, flashvars, params, attributes);

Here is also a working example: navigateToURL example.
And you can download the sourcecode.

cheers

Sorry, no comments at this time.