Tag Archives: navigateToURL

getting past IE popup blocker ActionScript3

can use this fix to bypass popups in IE…


/*

use this snippet in the as file to work with this class
 function  discoverBtnHandler(e:MouseEvent){

var popup:PassPopup = new PassPopup();
 // Open the popup window.
 popup.ChangePage( “http://blog.flashbanneronline.com/” );
 }

*/

package com {
 import flash.external.ExternalInterface;
 import flash.net.*;

public class PassPopup {
 public function ChangePage(url:*, window:String = “_blank”):void {
 var req:URLRequest = url is String ? new URLRequest(url) : url;
 if (!ExternalInterface.available) {
 navigateToURL(req, window);
 } else {
 var strUserAgent:String =  String(ExternalInterface.call(”function() {return  navigator.userAgent;}”)).toLowerCase();
 if (strUserAgent.indexOf(”firefox”) != -1 ||  (strUserAgent.indexOf(”msie”) != -1 &&  uint(strUserAgent.substr(strUserAgent.indexOf(”msie”) + 5, 3)) >= 7))  {
 ExternalInterface.call(”window.open”, req.url, window);
 } else {
 navigateToURL(req, window);
 }
 }
 }

}

}

and to use :


import com.neondust.external.PopupWindow;
 import com.neondust.external.PopupWindowDefaults;

var popup:PopupWindow = new PopupWindow();
 popup.width = WIDTH OF POPUP;
 popup.height = HEIGHT OF POPUP;
 popup.menuBar = false;
 popup.toolBar = false;
 popup.open( “YOUR HTML PAGE PATH” );

PopupWindow.as

//------------------------------------------------------------------------------
//
// COPYRIGHT © 2008 NEONDUST STUDIOS. ALL RIGHTS RESERVED.
//
//------------------------------------------------------------------------------
package com.neondust.external
{
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;

	/**
	 */
	public class PopupWindow
	{
		//----------------------------------------------------------------------
		//
		// PUBLIC CONSTANTS
		//
		//----------------------------------------------------------------------

		/**
		 * The name of the JavaScript function that will be used to open
		 * the popup window. The function should have the following signature:
		 *
		 * openPopupWindow( uri, name, opts )
		 */
		public static const OPEN_WINDOW_FUNCTION:String = "openPopupWindow";

		//----------------------------------------------------------------------
		//
		// PUBLIC PROPERTIES
		//
		//----------------------------------------------------------------------

		/**
		 * The x position of the popup window, in pixels. If this value is
		 * less than 0 the x position of the popup is set by the browser.
		 *
		 * @default -1
		 */
		public var x:int = PopupWindowDefaults.x;

		/**
		 * The y position of the popup window, in pixels. If this value is
		 * less than 0 the y position of the popup is set by the browser.
		 *
		 * @default -1
		 */
		public var y:int = PopupWindowDefaults.y;

		/**
		 * The width of the popup window, in pixels. If this value is
		 * less than 0 the width of the popup is set by the browser.
		 *
		 * @default -1
		 */
		public var width:int = PopupWindowDefaults.width;

		/**
		 * The height of the popup window, in pixels. If this value is
		 * less than 0 the height of the popup is set by the browser.
		 *
		 * @default -1
		 */
		public var height:int = PopupWindowDefaults.height;

		/**
		 * Indicates if the popup window menu bar should be displayed.
		 *
		 * @default false
		 */
		public var menuBar:Boolean = PopupWindowDefaults.menuBar;

		/**
		 * Indicates if the popup window tool bar should be displayed.
		 *
		 * @default false
		 */
		public var toolBar:Boolean = PopupWindowDefaults.toolBar;

		/**
		 * Indicates if the popup window location bar should be displayed.
		 *
		 * @default false
		 */
		public var locationBar:Boolean = PopupWindowDefaults.locationBar;

		/**
		 * Indicates if the popup window status bar should be displayed.
		 *
		 * @default false
		 */
		public var statusBar:Boolean = PopupWindowDefaults.statusBar;

		/**
		 * Indicates if the popup window scroll bars should be displayed.
		 *
		 * @default false
		 */
		public var scrollBars:Boolean = PopupWindowDefaults.scrollBars;

		/**
		 * Indicates if the popup window should be resizable.
		 *
		 * @default false
		 */
		public var resizable:Boolean = PopupWindowDefaults.resizable;

		//----------------------------------------------------------------------
		//
		// PRIVATE PROPERTIES
		//
		//----------------------------------------------------------------------

		/**
		 * @private
		 */
		private static var nextID:uint = 0;

		/**
		 * @private
		 */
		private var _uri:String = "";

		/**
		 * @private
		 */
		private var _name:String = "";

		//----------------------------------------------------------------------
		//
		// CONSTRUCTOR
		//
		//----------------------------------------------------------------------

		/**
		 */
		public function PopupWindow()
		{
			_name = "popupwindow_" + ( nextID ++ );
		}

		//----------------------------------------------------------------------
		//
		// PUBLIC METHODS
		//
		//----------------------------------------------------------------------

		/**
		 * Attempts to open a new popup window.
		 *
		 * @param uri
		 *        The URI of the page or file to load into the popup window.
		 */
		public function open( uri:String ):void
		{
			_uri = uri;

			var result:Boolean = false;

			if( ExternalInterface.available )
			{
				var func:String = OPEN_WINDOW_FUNCTION;
				var opts:String = optionsToString();

				result = ExternalInterface.call( func, uri, name, opts ) as Boolean;
			}

			if( result == false )
			{
				navigate();
			}
		}

		//----------------------------------------------------------------------
		//
		// PRIVATE METHODS
		//
		//----------------------------------------------------------------------

		/**
		 * @private
		 */
		private function navigate():void
		{
			var request:URLRequest = new URLRequest( uri );
			navigateToURL( request, "_blank" );
		}

		/**
		 * @private
		 */
		private function optionsToString():String
		{
			var opts:Array = [];

			if( x > -1 )
			{
				opts.push( "left=" + x );
			}

			if( y > -1 )
			{
				opts.push( "top=" + y );
			}

			if( width > -1 )
			{
				opts.push( "width=" + width );
				opts.push( "innerWidth=" + width );
			}

			if( height > -1 )
			{
				opts.push( "height=" + height );
				opts.push( "innerHeight=" + height );
			}

			opts.push( "menubar="    + ( menuBar     ? "yes" : "no" ) );
			opts.push( "toolbar="    + ( toolBar     ? "yes" : "no" ) );
			opts.push( "location="   + ( locationBar ? "yes" : "no" ) );
			opts.push( "status="     + ( statusBar   ? "yes" : "no" ) );
			opts.push( "scrollbars=" + ( scrollBars  ? "yes" : "no" ) );
			opts.push( "resizable="  + ( resizable   ? "yes" : "no" ) );

			return opts.join( "," );
		}

		//----------------------------------------------------------------------
		//
		// GETTERS/SETTERS
		//
		//----------------------------------------------------------------------

		/**
		 * The URI that was passed to the open() method. This value will be
		 * an empty string until the open() method is first invoked.
		 */
		public function get uri():String // read-only
		{
			return _uri;
		}

		/**
		 * The name of the popup window. This value is set internally by the
		 * PopupWindow class.
		 */
		public function get name():String // read-only
		{
			return _name;
		}

	}
}

PopupWindowDefaults.as

//------------------------------------------------------------------------------
//
// COPYRIGHT © 2008 NEONDUST STUDIOS. ALL RIGHTS RESERVED.
//
//------------------------------------------------------------------------------
package com.neondust.external
{
	/**
	 */
	public final class PopupWindowDefaults
	{
		//----------------------------------------------------------------------
		//
		// PUBLIC PROPERTIES
		//
		//----------------------------------------------------------------------

		/**
		 */
		public static var x:int = -1;

		/**
		 */
		public static var y:int = -1;

		/**
		 */
		public static var width:int = -1;

		/**
		 */
		public static var height:int = -1;

		/**
		 */
		public static var menuBar:Boolean = false;

		/**
		 */
		public static var toolBar:Boolean = false;

		/**
		 */
		public static var locationBar:Boolean = false;

		/**
		 */
		public static var statusBar:Boolean = false;

		/**
		 */
		public static var scrollBars:Boolean = false;

		/**
		 */
		public static var resizable:Boolean = false;

	}
}

//------------------------------------------------------------------------------
//
// COPYRIGHT © 2008 NEONDUST STUDIOS. ALL RIGHTS RESERVED.
//
//------------------------------------------------------------------------------
package com.neondust.external
{
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;

	/**
	 */
	public class PopupWindow
	{
		//----------------------------------------------------------------------
		//
		// PUBLIC CONSTANTS
		//
		//----------------------------------------------------------------------

		/**
		 * The name of the JavaScript function that will be used to open
		 * the popup window. The function should have the following signature:
		 *
		 * openPopupWindow( uri, name, opts )
		 */
		public static const OPEN_WINDOW_FUNCTION:String = "openPopupWindow";

		//----------------------------------------------------------------------
		//
		// PUBLIC PROPERTIES
		//
		//----------------------------------------------------------------------

		/**
		 * The x position of the popup window, in pixels. If this value is
		 * less than 0 the x position of the popup is set by the browser.
		 *
		 * @default -1
		 */
		public var x:int = PopupWindowDefaults.x;

		/**
		 * The y position of the popup window, in pixels. If this value is
		 * less than 0 the y position of the popup is set by the browser.
		 *
		 * @default -1
		 */
		public var y:int = PopupWindowDefaults.y;

		/**
		 * The width of the popup window, in pixels. If this value is
		 * less than 0 the width of the popup is set by the browser.
		 *
		 * @default -1
		 */
		public var width:int = PopupWindowDefaults.width;

		/**
		 * The height of the popup window, in pixels. If this value is
		 * less than 0 the height of the popup is set by the browser.
		 *
		 * @default -1
		 */
		public var height:int = PopupWindowDefaults.height;

		/**
		 * Indicates if the popup window menu bar should be displayed.
		 *
		 * @default false
		 */
		public var menuBar:Boolean = PopupWindowDefaults.menuBar;

		/**
		 * Indicates if the popup window tool bar should be displayed.
		 *
		 * @default false
		 */
		public var toolBar:Boolean = PopupWindowDefaults.toolBar;

		/**
		 * Indicates if the popup window location bar should be displayed.
		 *
		 * @default false
		 */
		public var locationBar:Boolean = PopupWindowDefaults.locationBar;

		/**
		 * Indicates if the popup window status bar should be displayed.
		 *
		 * @default false
		 */
		public var statusBar:Boolean = PopupWindowDefaults.statusBar;

		/**
		 * Indicates if the popup window scroll bars should be displayed.
		 *
		 * @default false
		 */
		public var scrollBars:Boolean = PopupWindowDefaults.scrollBars;

		/**
		 * Indicates if the popup window should be resizable.
		 *
		 * @default false
		 */
		public var resizable:Boolean = PopupWindowDefaults.resizable;

		//----------------------------------------------------------------------
		//
		// PRIVATE PROPERTIES
		//
		//----------------------------------------------------------------------

		/**
		 * @private
		 */
		private static var nextID:uint = 0;

		/**
		 * @private
		 */
		private var _uri:String = "";

		/**
		 * @private
		 */
		private var _name:String = "";

		//----------------------------------------------------------------------
		//
		// CONSTRUCTOR
		//
		//----------------------------------------------------------------------

		/**
		 */
		public function PopupWindow()
		{
			_name = "popupwindow_" + ( nextID ++ );
		}

		//----------------------------------------------------------------------
		//
		// PUBLIC METHODS
		//
		//----------------------------------------------------------------------

		/**
		 * Attempts to open a new popup window.
		 *
		 * @param uri
		 *        The URI of the page or file to load into the popup window.
		 */
		public function open( uri:String ):void
		{
			_uri = uri;

			var result:Boolean = false;

			if( ExternalInterface.available )
			{
				var func:String = OPEN_WINDOW_FUNCTION;
				var opts:String = optionsToString();

				result = ExternalInterface.call( func, uri, name, opts ) as Boolean;
			}

			if( result == false )
			{
				navigate();
			}
		}

		//----------------------------------------------------------------------
		//
		// PRIVATE METHODS
		//
		//----------------------------------------------------------------------

		/**
		 * @private
		 */
		private function navigate():void
		{
			var request:URLRequest = new URLRequest( uri );
			navigateToURL( request, "_blank" );
		}

		/**
		 * @private
		 */
		private function optionsToString():String
		{
			var opts:Array = [];

			if( x > -1 )
			{
				opts.push( "left=" + x );
			}

			if( y > -1 )
			{
				opts.push( "top=" + y );
			}

			if( width > -1 )
			{
				opts.push( "width=" + width );
				opts.push( "innerWidth=" + width );
			}

			if( height > -1 )
			{
				opts.push( "height=" + height );
				opts.push( "innerHeight=" + height );
			}

			opts.push( "menubar="    + ( menuBar     ? "yes" : "no" ) );
			opts.push( "toolbar="    + ( toolBar     ? "yes" : "no" ) );
			opts.push( "location="   + ( locationBar ? "yes" : "no" ) );
			opts.push( "status="     + ( statusBar   ? "yes" : "no" ) );
			opts.push( "scrollbars=" + ( scrollBars  ? "yes" : "no" ) );
			opts.push( "resizable="  + ( resizable   ? "yes" : "no" ) );

			return opts.join( "," );
		}

		//----------------------------------------------------------------------
		//
		// GETTERS/SETTERS
		//
		//----------------------------------------------------------------------

		/**
		 * The URI that was passed to the open() method. This value will be
		 * an empty string until the open() method is first invoked.
		 */
		public function get uri():String // read-only
		{
			return _uri;
		}

		/**
		 * The name of the popup window. This value is set internally by the
		 * PopupWindow class.
		 */
		public function get name():String // read-only
		{
			return _name;
		}

	}
}

AS3 Linking to External WebPages

This tutorial will teach you how to use ActionScript 3.0 to link from your Flash project to another webpage using the navigateToURL() method.

Using the navigateToURL() Method

Linking to another webpage in AS3 is achieved by using the navigateToURL() method. This method is used in the following format:

navigateToURL(myURL, window);

Where the URL is passed as an instance of the URLRequest Class and the window is the target window to open the link. It must remembered that you cannot pass the URL directly to the navigateToURL() method as it must be first passed through a URLRequest instance. The example below shows an actual example on how this is achieved:

var myURL:URLRequest = new URLRequest(“http://www.flashbanneronline.com/”);
navigateToURL(myURL, “_blank”);

You can instantiate your URLRequest inside your navigateToURL() to write less code:

navigateToURL(new URLRequest(“http://www.flashbanneronline.com/”), “_blank”);

The window argument at the end is optional. You can also remove it if you want the link to use the default option which is to open in the same page that hosts the Flash movie.

navigateToURL(new URLRequest(“http://www.flashbanneronline.com/”));

However, if you want to explicitly state that you want the link to open in the same window then you can use the _same as your value:

navigateToURL(new URLRequest(“http://www.flashbanneronline.com/”), “_same”);

Other options available as “_blank” to open the link in a blank new window or to write the name of whatever window you have if you are using frames layout on your website.

Practical Example

In this section we will create a simple example in which a button will be created to link to another website.

Start off by creating a new FLA in AS3 format.

AS3 - Link URL New FLA

Go through Window>Component to open the Components panel. Expand the User Interface category and drag an instance of the Button component to your stage. You can go through Windows>Components Inspector to open this panel and configure the Label of your button.

AS3 Link Button on Stage

Now select your button and then access the Properties Inspector to assign the instance name my_btn to this button. Using instance names makes it easy for us to refer to our objects using ActionScript.

AS3 Instance Name Link

Our button is now ready, it is time to start the ActionScript. Right-click the only frame you have on your timeline and select Actions to open up the Actions panel.

In order to command our button to execute any action, we need to use an event listener registered with the MouseEvent.CLICK. This event listener will invoke a function called onClick which we have defined, without any actual content, below:

my_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void{

}

We will tell our onClick listener function to call the navigateToURL() method to open a link to our website this way:

my_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void{
navigateToURL(new URLRequest(“http://www.flashbanneronline.com/”), “_blank”);
}

That should do it. You can now test your movie by going through Control>Test Movie to see you functional button in action!

Special Thanks :RepublicOfCode