AS3 getURL solved

February 6th, 2008 by Steven Sacks

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:

Download AS3 GetURL

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

17 Responses

  1. Johnny

    serious? You need to use this class?

  2. Steven Sacks

    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.

  3. matt

    nice…I'll be using this. thanks

  4. Quentin

    If you also have an idea on how to avoid new windows being blocked by browsers (Firefox and IE), it'd be awesome!

  5. flashlizi

    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

  6. Saturday Night Special

    Nice little class .. Thanks! Gaia looks sweet too.

  7. BrianG

    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!

  8. Immo Blaese

    Nice short cut, works really well for me, thanks.

  9. matt

    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…

  10. Ryan

    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?

  11. Steven Sacks

    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.

    :)

  12. Ryan

    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.

  13. Steven Sacks

    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!
    8)

  14. Roberto

    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.

  15. Steven Sacks

    import net.stevensacks.utils.Web;

    Web.getURL("some.html");

  16. saul

    Great job Mr. Sacks!

  17. Apollo

    Nice job. Simple & elegant. Will come in handy for a project I'm working on. Thanks!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Steven Sacks

I am a professional Flash developer with over 13 years of programming experience. I have consulted for high-profile agencies and companies in San Francisco, Los Angeles, Atlanta and New York, and developed numerous award-winning websites and rich internet applications for clients including Adobe, Fox Sports, FX Networks, Anheuser-Busch, GE, DirecTV, ESPN, The Weather Channel, Home Depot, and Coca-Cola.

I am the author of the open-source Gaia Framework for Adobe Flash, which dramatically reduces development time and makes developing Flash sites much easier.