By: Jonathan Marecki
I have only been programming in Actionscript 3 and Flex 2 since January. I studied C++ and experimented with Flash and Actionscript 2 while I was in college, but that was about as far as my knowledge went. When I started learning Flex, I didn’t even understand what an event was.
As I learned, I relied on others who were knowledgeable and were willing to share what they knew. Now it is my turn to share what I know with the Flex community.
I’m going to start by showing how to handle links that are clicked inside your Flex application, and need to open in a new tab.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import flash.net.*;
public function handleLink():void {
var newLink:URLRequest = new URLRequest(url.text);
navigateToURL(newLink,"_blank");
}
]]>
</mx:Script>
<mx:Text id="url" text="http://yahoo.com"/>
<mx:Button label="Open New Window" click="handleLink()"/>
</mx:Application>
When the button is clicked, the handleLink function is called. You can open the new site using the navigateToURL function, by passing in a
URLRequest Object. The advantage of having a function open the new site is that you can do error checking on the URL or even add variables to the URL.
var newLink:URLRequest = new URLRequest(url.text+"/r/24");
Adding “/r/24″ to the end of the URL will take you to Yahoo maps instead of taking you to Yahoo’s main page.
The disadvantage of this method is that in some browsers, this new tab will be considered a pop-up and will be blocked until the user gives their approval.
Another way to handle a link is to use the ‘ExternalInterface’ class. This class allows Flash to communicate directly with the HTML container using Javascript calls.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
import flash.external.ExternalInterface;
<mx:Script>
<![CDATA[
import flash.net.*;
public function handleLink():void {
ExternalInterface.call("window.open",url.text,"_blank");
}
]]>
</mx:Script>
<mx:Text id="url" text="http://yahoo.com"/>
<mx:Button label="Open New Window" click="handleLink()"/>
</mx:Application>
