2 // Written by Mark Perkins, mark@allmarkedup.com
3 // License: http://unlicense.org/ (i.e. do what you want with it!)
5 jQuery.url = function()
12 * Options object. Only the URI and strictMode values can be changed via the setters below.
16 url : window.location, // default URI is the page in which the script is running
18 strictMode: false, // 'loose' parsing by default
20 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], // keys available to query
24 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
28 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, //less intuitive, more accurate to the specs
29 loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
35 * Deals with the parsing of the URI according to the regex above.
36 * Written by Steven Levithan - see credits at top.
38 var parseUri = function()
40 str = decodeURI( options.url );
42 var m = options.parser[ options.strictMode ? "strict" : "loose" ].exec( str );
47 uri[ options.key[i] ] = m[i] || "";
50 uri[ options.q.name ] = {};
51 uri[ options.key[12] ].replace( options.q.parser, function ( $0, $1, $2 ) {
53 uri[options.q.name][$1] = $2;
61 * Returns the value of the passed in key from the parsed URI.
63 * @param string key The key whose value is required
65 var key = function( key )
67 if ( jQuery.isEmptyObject(parsed) )
69 setUp(); // if the URI has not been parsed yet then do this first...
73 if ( parsed.port !== null && parsed.port !== "" )
75 return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/";
79 return parsed.protocol+"://"+parsed.host+"/";
83 return ( parsed[key] === "" ) ? null : parsed[key];
87 * Returns the value of the required query string parameter.
89 * @param string item The parameter whose value is required
91 var param = function( item )
93 if ( jQuery.isEmptyObject(parsed) )
95 setUp(); // if the URI has not been parsed yet then do this first...
97 return ( parsed.queryKey[item] === null ) ? null : parsed.queryKey[item];
101 * 'Constructor' (not really!) function.
102 * Called whenever the URI changes to kick off re-parsing of the URI and splitting it up into segments.
104 var setUp = function()
112 * Splits up the body of the URI into segments (i.e. sections delimited by '/')
114 var getSegments = function()
117 segments = []; // clear out segments array
118 segments = parsed.path.length == 1 ? {} : ( p.charAt( p.length - 1 ) == "/" ? p.substring( 1, p.length - 1 ) : path = p.substring( 1 ) ).split("/");
124 * Sets the parsing mode - either strict or loose. Set to loose by default.
126 * @param string mode The mode to set the parser to. Anything apart from a value of 'strict' will set it to loose!
128 setMode : function( mode )
130 options.strictMode = mode == "strict" ? true : false;
135 * Sets URI to parse if you don't want to to parse the current page's URI.
136 * Calling the function with no value for newUri resets it to the current page's URI.
138 * @param string newUri The URI to parse.
140 setUrl : function( newUri )
142 options.url = newUri === undefined ? window.location : newUri;
148 * Returns the value of the specified URI segment. Segments are numbered from 1 to the number of segments.
149 * For example the URI http://test.com/about/company/ segment(1) would return 'about'.
151 * If no integer is passed into the function it returns the number of segments in the URI.
153 * @param int pos The position of the segment to return. Can be empty.
155 segment : function( pos )
157 if ( jQuery.isEmptyObject(parsed) )
159 setUp(); // if the URI has not been parsed yet then do this first...
161 if ( pos === undefined )
163 return segments.length;
165 return ( segments[pos] === "" || segments[pos] === undefined ) ? null : segments[pos];
168 attr : key, // provides public access to private 'key' function - see above
170 param : param // provides public access to private 'param' function - see above