AS3 clickTag & IE popup blocker

Recently, we at Gynzy had to implement clickTag scripts in Actionscript 3 banners. As you might know, AS 3 banners don’t work well in Internet Explorer. You’ll get a pop-up blocked warning when using navigateToURL().

After some googling I found a clickTag script at
blog.advalidation.com/2011/06/as3-clicktag.html
It uses ExternalInterface.call('window.open', url, '_blank'); when the user agent of the banner is MSIE. This script worked almost perfect, it only had two requirements:
1) AllowScriptAccess needs to be true either by setting it to “always” or by setting it to “sameDomain” and loading the flash from the same domain as the hosting page.
2) The .swf file has to be embedded in html.

If the requirements are not met, actionscript will throw an error without opening the requested url (only in Internet Explorer of course). I tried to work around this problem by catching the error and use navigateToURL in case ExternalInterface.call fails. See my script below:

function handleClick(mouseEvent:MouseEvent):void {
var interactiveObject:InteractiveObject = mouseEvent.target as InteractiveObject;
  var li:LoaderInfo = LoaderInfo(interactiveObject.root.loaderInfo);
  var url:String = li.parameters.clickTag;
  if (url) {
    if (ExternalInterface.available) {
      try {
        var userAgent:String = ExternalInterface.call('function(){ return navigator.userAgent; }');
        if (userAgent && userAgent.indexOf("MSIE") >= 0) {
          ExternalInterface.call('window.open', url, '_blank');
          return;
        }
      } catch (e:Error) {
        //
      }
    }
    navigateToURL(new URLRequest(url), '_blank');
  }
}
myButton_btn.addEventListener(MouseEvent.CLICK, handleClick);

To test my script in Internet Explorer, open: as3_clicktag.html
(ideal situation, script will use ExternalInterface.call)

The same banner with allowscriptaccess set to never: as3_clicktag_no_scriptaccess.html
(script will use navigateToURL instead of ExternalInterface.call)

And the swf file without html: as3_clicktag_fixed_url.swf

In case you’re interested in the source files: as3_clicktag.zip

3 responses to “AS3 clickTag & IE popup blocker”

  1. Sean

    You are a genius, my friend! Awesome! This works perfectly.

  2. Alex

    You’ll want to update this code for the IE11 update as the userAgent is now “Triton”.

  3. DonZottel

    @Alex

    import flash.external.ExternalInterface;

    var clickUrl:String = new String(root.loaderInfo.parameters.clickTag);
    var clickTarget:String = new String(root.loaderInfo.parameters.clicktarget);
    var clickUrlRequest:URLRequest = new URLRequest(clickUrl);

    function handleClick(event:MouseEvent):void {
    if (ExternalInterface.available) {
    var isIe:Boolean = ExternalInterface.call(“function () { return /Trident/.test(navigator.userAgent) }”);
    if (isIe) {
    ExternalInterface.call(“window.open”, clickUrl, clickTarget);
    } else {
    navigateToURL(clickUrlRequest, clickTarget);
    }
    } else {
    navigateToURL(clickUrlRequest, clickTarget);
    }
    };

Leave a Reply