URL AS3 Class

Custom class dependancies: QueryParameters

This class is an attempt to have an object that automatically parses any string representing a url. Similar to the way the javascript object window.location parses the window’s url. The url string is supplied either through the constructor, or through setting the ‘href’ property. What is important to note, is that all properties are read and write, and effect the values of all the other respective properties. Here is a list of all the URL properties:

  • href – Specifies the entire URL.
  • protocol – Specifies the beginning of the URL, including the colon.
  • host – Specifies the host and domain name, or IP address, of a network host.
  • hostname – Specifies the host of the hostname:port portion of the URL.
  • port – Specifies the communications port that the server uses.
  • pathname – Specifies the URL-path portion of the URL.
  • search – Specifies a query.
  • hash – Specifies an anchor name in the URL.

Please play around with the example below to get an idea of how this class works:

AS3 URL Example735 downloads

Now here is the source of the class itself. Please make sure to review the QueryParameters class for additional information on this class.

Comment if you have any questions, or suggestions.

4 thoughts on “URL AS3 Class

  1. Hello dazz,

    I took a look at the code at your link and I think you are trying to allow your swf to play only locally and from one particular host. Correct?
    If this is the case, you shouldn’t have to mess with parsing the url, a more simple and secure way would be to put a “crossdomain.xml” file on your server, check this:
    http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_14213

    If you truely want to do it with domain parsing, then all you have to check is if the player is within an html document, in the stand alone player, or playing in the browser locally. To do that, check if ExternalInterface is available, like so:

    var acceptedHosts:String = "www.mydomain.com,www.mydomain2.com";
    var isLocal:Boolean;

    // use a try-catch block because the 'available' property is buggy
    if(ExternalInterface.available) {
    try {
    isLocal = (ExternalInterface.call("window.location.protocol.toString") == "file:");
    } catch (error) {
    isLocal = true;
    }
    }

    if(!isLocal) {
    if(acceptedHosts.indexOf(ExternalInterface.call("window.location.hostname.toString")) >= 0) {
    // HOST IS VALID, PLAY THE FILE!
    }
    } else {
    // IT'S LOCAL, PLAY THE FILE!
    }

    For more information on working with the window Javascript object through AS3, take a look that this post:
    http://jidd.jimisaacs.com/archives/187

    Try it out, and let me know.

  2. Gabriel,

    First off, thanks for pointing out the stray var declarations, random stranger or not ;) I’ll fix that soon. They were left overs when I started writing this thing.

    Second of all, I know and use URI from the corelib, but if you’ve noticed it’s a bit overkill to be a simple URI parser. My class is meant to only be used as a String parser, nothing more, which is what I wanted.
    The URI class from the corelib also follows this pattern:
    <scheme>:<authority><path>?<query>#<fragment>

    Which is right, though mine follows the Javascript pattern, which is also right, and more widely used in the window Object. In top-level web scripting I hate having to learn things double, and most of the times three, four, and tens times again and again. When people don’t just decide if something is better, they just decide to make something different because they either felt like it, or because they though it was better on that particular day. Then when they make their code popular by distributing it, their usage becomes common practice as well, as any authors doing the same thing. I made this just because of the preference I have for the time being :)

    In the URI class, I do like their use of RegExp, though it is more suited for their pattern. I am of course a Regular Expression enthusiast, but I damn well can write a pattern for all the possibilities in – protocol//host:port/pathname?search#hash

    If you or anyone else can, I’m all ears, and will incorporate in this class! Or just simply show me a stripped down lightweight version of the bloated URI class (if I don’t beat you to it ;) )

    Thanks

    P.S. I realize also the pattern in the Class header comments say:
    protocol//host:port/pathname#hash?search

    I simply forgot to change this, when I decided to reverse the ‘hash’ and ‘search’ order to be more useful. I’ll change this when I get rid those declarations. Thanks again…

Leave a Reply