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

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.