QuickTip: Avoid Firefox popup-blocker When Using navigateToUrl

This entry is part of a series, Quick Tips»

Being faced with this when you’re trying to launch an other site is not good! Read on to learn how to fix it.

Solving this problem is in fact not very hard when you understand why it’s happening. Firefox only allows your SWF to open new links when there is a user input. If they didn’t do this, agressive ads could open up URL’s without a user input. So if you are executing the navigateToURL in a Event.ENTER_FRAME loop (or somethign similar), you need to change your code.

 

navigateToURL

For handling navigateToUrl I’m using a custom class I wrote ‘LinkUtil‘, that has the following two functions:

public static function OpenUrlBlank ( url:String ) :void
{
	var urlReq:URLRequest = new URLRequest ( url ) ;
	navigateToURL ( urlReq, "_blank" ) ;
}
 
public static function OpenUrlSelf ( url:String ) :void
{
	var urlReq:URLRequest = new URLRequest ( url ) ;
	navigateToURL ( urlReq, "_self" ) ;
}

The fact that I’m using the LinkUtil to make the navigateToURL call doesn’t make any difference.. it’s just a handy way of doing it.

 

For Keyboard

First you need to add an event listener for for KEY_UP:

this.stage.addEventListener ( KeyboardEvent.KEY_UP, onKeyRelease  ) ;

then create these functions two functions. One should figure out if the selected menu item has an outgoing link (and return a Boolean), the other should find such a link (and return it as a String).

Then all there is left to do is create the event handler.

private function onKeyRelease (e:KeyboardEvent):void 
{
	if ( (e.keyCode == Keyboard.ENTER || e.keyCode == Keyboard.SPACE ) && selectedIsLink() )
	{
		LinkUtil.OpenUrlBlank ( getSelectedUrl() ) ;
	}
}
 
// remember to:
// import com.rasmuswriedtlarsen.utils.LinkUtil;
// import flash.ui.Keyboard;

If you don’t want to use space and/or enter, just delete/replace them.

 

For mouse

Implement something like this:

// add this to the constructor function, or somewhere else
this.addEventListener ( MouseEvent.CLICK, onClick ) ;
 
// this is the event handler:
private function onClick(e:MouseEvent):void 
{
	LinkUtil.OpenUrlBlank( "http://google.com" ) ;
}
 
// remember to:
// import com.rasmuswriedtlarsen.utils.LinkUtil;

 

Happy coding :)

Posted on December 7th, 2010
Filed under ActionScript 3, Flash, Tutorial, User Interface, Util |

Leave a Reply