X-Git-Url: http://www.average.org/gitweb/?p=mkgallery.git;a=blobdiff_plain;f=include%2Furlparser.js;fp=include%2Furlparser.js;h=cbcdda9aa34e041c73bbcb4a4173efcb447f74c8;hp=0000000000000000000000000000000000000000;hb=224aa7103c52952fe867c36d3cb4f7217f4fbc4c;hpb=7104bfdf4b25fc5bc8e8aaf9e65e6846a731bebc diff --git a/include/urlparser.js b/include/urlparser.js new file mode 100644 index 0000000..cbcdda9 --- /dev/null +++ b/include/urlparser.js @@ -0,0 +1,30 @@ + + +/************************************************************** + + Script : URL Parser + Version : 1.0 + Authors : Steven Levithan + Desc : Splits any well-formed URL + +**************************************************************/ + +function parseUrl(sourceUrl){ + var urlPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"]; + var urlParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUrl); + var url = {}; + + for(var i = 0; i < 10; i++){ + url[urlPartNames[i]] = (urlParts[i] ? urlParts[i] : ""); + } + + // Always end directoryPath with a trailing backslash if a path was present in the source URI + // Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key + if(url.directoryPath.length > 0){ + url.directoryPath = url.directoryPath.replace(/\/?$/, "/"); + } + + return url; +} + +/*************************************************************/