Skip to content Skip to sidebar Skip to footer

Python Urljoin Equivalent In As3

Python has a function urljoin that takes two URLs and concatenates them intelligently. Is there a library that provides a similar function in AS3? urljoin documentation: http://doc

Solution 1:

You can use for example the URI class from as3corelib

Usage:

import com.adobe.net.URI;

// create uri you want to be updatedvar newURI:URI=new URI('../res/jer.png')

// update newURI with the full path
newURI.makeAbsoluteURI(new URI('http://www.cwi.nl/doc/Python.html')) 

trace(uri.toString()) // will output http://www.cwi.nl/res/jer.png

// or make an utility function base on it:functionurljoin(url1:string, url2:String):String {
  varuri:URI=newURI(url2)
  uri.makeAbsoluteURI(url1)
  return uri.toString()
}

Solution 2:

Some raw code doing what you want, so you can skip all of the bloated libraries:

var urlJoin:Function = function(base:String, relative:String):String
{
    // See if there is already a protocol on thisif (relative.indexOf("://") != -1)
        return relative;

    // See if this is protocol-relativeif (relative.indexOf("//") == 0)
    {
        var protocolIndex:int = base.indexOf("://");
        returnbase.substr(0, protocolIndex+1) + relative;
    }

    // We need to split the domain and the path for the remaining optionsvar protocolIndexEnd:int = base.indexOf("://") + 3;
    if (base.indexOf("/", protocolIndexEnd) == -1) // append slash if passed only http://bla.combase += "/";
    var endDomainIndex:int = base.indexOf("/", protocolIndexEnd);
    var domain:String = base.substr(0, endDomainIndex);
    var path:String = base.substr(endDomainIndex);
    if (path.lastIndexOf("/") != path.length-1) // trim off any ending file name
        path = path.substr(0, path.lastIndexOf("/")+1);

    // See if this is site-absoluteif (relative.indexOf("/") == 0)
    {
        return domain + relative;
    }

    // See if this is document-relative with ../while (relative.indexOf("../") == 0)
    {
        relative = relative.substr(3);
        if (path.length > 1)
        {
            var secondToLastSlashIndex:int = path.substr(0, path.length-1).lastIndexOf("/");
            path = path.substr(0, secondToLastSlashIndex+1);
        }
    }   
    // Finally, slap on whatever ending is leftreturn domain + path + relative;
};

Post a Comment for "Python Urljoin Equivalent In As3"