AS3 getURL solved
One of the things that has become something of a pain in AS3 is getURL. It requires two imports and a try catch. I wrote a little class that simplifies it for me so I can save myself from having to write all that code anytime I want to link to an outside page. Here it is:
package net.stevensacks.utils
{
import flash.net.navigateToURL;
import flash.net.URLRequest;
public class Web
{
public static function getURL(url:String, window:String = null):void
{
var req:URLRequest = new URLRequest(url);
trace("getURL", url);
try
{
navigateToURL(req, window);
}
catch (e:Error)
{
trace("Navigate to URL failed", e.message);
}
}
}
}
Usage:
Web.getURL("someurl.html");
Posted in Actionscript, Flash, Tips/Tricks
February 6th, 2008 at 7:51 pm
serious? You need to use this class?
February 6th, 2008 at 8:15 pm
Need? It's called DRY. Why should I ever rewrite all this navigateToURL code? One static method in one place that does it all.
So, yes, I need to use this class in order to write clean, reusable code.
February 6th, 2008 at 10:50 pm
nice…I'll be using this. thanks
February 7th, 2008 at 1:17 am
If you also have an idea on how to avoid new windows being blocked by browsers (Firefox and IE), it'd be awesome!
February 21st, 2008 at 6:24 pm
to Quentin:
I give you a way to avoid new window blocked by Firefox/IE.
code:
function getURL(url:String,window:String="_blank"):void{ var broswer:String=ExternalInterface.call("function getBrowser(){return navigator.userAgent}") as String; if(broswer.indexOf("Firefox")!=-1 || broswer.indexOf("MSIE 7.0")!=-1){ ExternalInterface.call('window.open("'+url+'","'+window+'")'); }else{ navigateToURL(new URLRequest(url),window); }}
and the most important is set the wmode property is opaque or transparent.
for more information,you can see my blog(chinese):
http://www.riaidea.com/article.asp?id=27
February 29th, 2008 at 7:52 am
Nice little class .. Thanks! Gaia looks sweet too.
April 21st, 2008 at 4:02 pm
Sorry, bit of a n00b here. is that javascript for the page as a workaround to the flash getURL calls being popup blocked, or should that be used in the flash file to replace the default getURL function? thanks!
May 14th, 2008 at 2:26 am
Nice short cut, works really well for me, thanks.
May 15th, 2008 at 1:53 am
I think you might be over OOP-ing things… that whole class can be summed up in one line:
navigateToURL(new URLRequest('blah.com'), '_blank');
… sure you still have to include navigateToURL and URLRequest, but you'd have to either way… the 'try' function is nice, but it doesn't really add anything to be built in to the class like that… if you could pass in a onFail function, it'd help, I guess…
June 10th, 2008 at 6:24 am
to: matt
Good point. Steven is doing things right, but this kind of thing must confuse the hell out of people just starting out. Steven, maybe consider commenting on the one-line solution as well?
June 10th, 2008 at 11:20 am
First off, you must import flash.net.navigateToURL and flash.net.URLRequest. That's three lines, not one.
Second, navigateToURL can fail resulting in a runtime error if you don't try…catch it. Many developers aren't familiar with the strict rules of AS3 and continue to code in the loose style of AS1/AS2 without regard for null pointers and runtime errors.
Third, DRY is not OOP, it's just smart coding, whether you're writing procedural code or object oriented. If you write the same line(s) of code more than once, you should write a subroutine to handle it. It's programming 101.
And finally, it's a lot less code to import one class and write
Web.getURL("blah.com", "_blank") ;
than it is to import two classes and write
navigateToURL(new URLRequest("blah.com"), "_blank");
If you don't want to use this class, don't. If you don't see how this class saves coding time and makes getURL easier, oh well. If you think that Matt is right, use his method. It doesn't matter to me.
June 15th, 2008 at 10:57 am
Hey man, I'm not trying to say anyone is right. I'd use your class for sure. I guess my point was just that there's a lot of flash users out there that are doing really simple things and aren't really "programmers". Those kind of people are probably still throwing code on a frame in the timeline, and they wouldn't have to import the two classes or go as far as to use yours. So if someone is just making simple banners or something, they probably don't need to worry about really strict code. I guess your blog isn't really for those kind of people though, so I don't know what I'm really saying here.
June 15th, 2008 at 1:44 pm
But, it doesn't matter if the code is on the timeline or not. You have to import those two classes in order to use navigateToURL, that's why getURL isn't so easy anymore, and that's the very reason why importing my one class and not having to pass it a URLRequest yourself (just the url) makes getURL easy again!

June 21st, 2008 at 2:07 pm
Steven, I need to include this Class in my Flash File. Could it be possible to have an example of how to use it? An .FLA file? Thank you for your assistance.
June 21st, 2008 at 5:55 pm
import net.stevensacks.utils.Web;
Web.getURL("some.html");
July 1st, 2008 at 3:26 am
Great job Mr. Sacks!
July 19th, 2008 at 1:52 pm
Nice job. Simple & elegant. Will come in handy for a project I'm working on. Thanks!