URL AS3 Class

Custom class dependencies: QueryParameters

This class is an attempt to have an object that will automatically parse 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 affect 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 in the URL.

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

[swfobj src=”https://jidd.jimisaacs.com/files/2008/08/example-URL.swf” width=”500″ height=”200″ align=”center” required_player_version=”10.0.0″]

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.


Posted

in

, ,

by

Comments

5 responses to “URL AS3 Class”

  1. dazz Avatar
    dazz

    Hello,

    i’m browsing for a while to find out an answer (and it become urgent cause my boss seriously think i’m not so smart ๐Ÿ™ )
    the latest topic i made in adobe forum :
    http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=665&threadid=1404847&enterthread=y

    reading your page it seems you may be the one which can find a decent solution..

    Thanks a lot

    dazz

  2. jimisaacs Avatar

    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:
    https://jidd.jimisaacs.com/archives/187

    Try it out, and let me know.

  3. gabriel Avatar

    Hi,

    Cool code… Thanks for sharing. ๐Ÿ™‚

    You may want to check out mx.utils.URLUtil – might be helpful. Also, depending on your needs, perhaps the URI class @ http://code.google.com/p/as3corelib/source/browse/trunk/?r=67#trunk/src/com/adobe/net could provide a good reference.

    And in the interest of crazy points from random strangers… ๐Ÿ™‚ … you declare but don’t use _host – and _url is there, but you construct the _href.

    Thanks…!!

  4. jimisaacs Avatar

    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…

  5. eddie Avatar

    Great, thanks for sharing. I was about the write code that does the same thing.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.