1 /** 2 @class Parse URLs and query strings 3 @param url is the URL you would like to extract information from. 4 */ 5 6 /* 7 PHP style: 8 9 [scheme] => http 10 [host] => hostname 11 [port] => 2344 12 [user] => username 13 [pass] => password 14 [path] => /path 15 [query] => arg=value 16 [fragment] => anchor 17 */ 18 function URL(url) { 19 // inspired by some of the RegExp found here: http://rodneyrehm.de/t/url-regex.html 20 // The raw array of data extracted from the URL passed in. 21 var exploded = (new RegExp(/^(.*):\/\/((.*?)(:(.*?))@){0,1}([^/$.?#].[^\s]*?){0,1}(:(\d+)){0,1}([\/].*?){0,1}([\?].*?){0,1}([#](.*)){0,1}$/i)).exec(url); 22 23 /** Object containing an associative array of the components extracted from the URL (scheme, host, port, user, pass, path, hash, (raw) querystring). */ 24 this.components = { 25 scheme: exploded[1] || null, 26 host: exploded[6] || null, 27 port: exploded[8] ? parseInt(exploded[8]) : null, 28 user: exploded[3] || null, 29 pass: exploded[5] || null, 30 path: exploded[9] || null, 31 hash: exploded[12] || null, 32 querystring: exploded[10] || null 33 } 34 35 // make the components accessible on the URL object 36 var c = this.components; 37 /** URL scheme - "http", "https", "ftp", etc. */ 38 this.scheme = c["scheme"]; 39 /** Host domain of the URL - "google.com", "jsgamesoup.net" etc. */ 40 this.host = c["host"]; 41 /** Port the URL is accessing - 80, 8080, etc. */ 42 this.port = c["port"]; 43 /** HTTP auth username accessing this URL. */ 44 this.user = c["user"]; 45 /** HTTP auth password accessing this URL. */ 46 this.pass = c["pass"]; 47 /** Full path to the resource at the server - "/my/document/here.html" */ 48 this.path = c["path"]; 49 /** Hash anchor tag appended to this URL. - "myanchor" */ 50 this.hash = c["hash"]; 51 /** Raw query string. Use the get() method on the URL object to retrive individual values. */ 52 this.querystring = c["querystring"]; 53 54 // perform the extraction of the query string 55 // query string key value store 56 var qs = {}; 57 // original idea & RegExp from here: 58 // http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/ 59 if (exploded[10]) { 60 exploded[10].replace( 61 new RegExp("([^?=&]+)(=([^&]*))?", "g"), 62 function(whole, k, inc, v) { 63 var val = unescape(v); 64 // we already have this key defined 65 if (qs[k]) { 66 // is it already an array? just add our new element 67 if (typeof(qs[k]) == typeof([])) { 68 qs[k].push(val); 69 } else { 70 // create the array with our old and new values 71 qs[k] = [qs[k], val]; 72 } 73 } else { 74 qs[k] = val; 75 } 76 } 77 ); 78 } 79 80 /** Key/value store of the parsed query string. Multiple keys of the same value will result in an array. */ 81 this.query = qs; 82 83 /** @method 84 Returns the parsed query string value for a particular key. 85 @param key is the key to search on. 86 @return the value corresponding to that key. If there are multiple values for any key this will yield and array. 87 */ 88 this.get = function(key) { 89 if (key in qs) { 90 return qs[key]; 91 } else { 92 return null; 93 } 94 } 95 } 96