jquery.url.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // JQuery URL Parser
  2. // Written by Mark Perkins, mark@allmarkedup.com
  3. // License: http://unlicense.org/ (i.e. do what you want with it!)
  4. jQuery.url = function()
  5. {
  6. var segments = {};
  7. var parsed = {};
  8. /**
  9. * Options object. Only the URI and strictMode values can be changed via the setters below.
  10. */
  11. var options = {
  12. url : window.location, // default URI is the page in which the script is running
  13. strictMode: false, // 'loose' parsing by default
  14. key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], // keys available to query
  15. q: {
  16. name: "queryKey",
  17. parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  18. },
  19. parser: {
  20. strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, //less intuitive, more accurate to the specs
  21. loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
  22. }
  23. };
  24. /**
  25. * Deals with the parsing of the URI according to the regex above.
  26. * Written by Steven Levithan - see credits at top.
  27. */
  28. var parseUri = function()
  29. {
  30. str = decodeURI( options.url );
  31. var m = options.parser[ options.strictMode ? "strict" : "loose" ].exec( str );
  32. var uri = {};
  33. var i = 14;
  34. while ( i-- ) {
  35. uri[ options.key[i] ] = m[i] || "";
  36. }
  37. uri[ options.q.name ] = {};
  38. uri[ options.key[12] ].replace( options.q.parser, function ( $0, $1, $2 ) {
  39. if ($1) {
  40. uri[options.q.name][$1] = $2;
  41. }
  42. });
  43. return uri;
  44. };
  45. /**
  46. * Returns the value of the passed in key from the parsed URI.
  47. *
  48. * @param string key The key whose value is required
  49. */
  50. var key = function( key )
  51. {
  52. if ( jQuery.isEmptyObject(parsed) )
  53. {
  54. setUp(); // if the URI has not been parsed yet then do this first...
  55. }
  56. if ( key == "base" )
  57. {
  58. if ( parsed.port !== null && parsed.port !== "" )
  59. {
  60. return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/";
  61. }
  62. else
  63. {
  64. return parsed.protocol+"://"+parsed.host+"/";
  65. }
  66. }
  67. return ( parsed[key] === "" ) ? null : parsed[key];
  68. };
  69. /**
  70. * Returns the value of the required query string parameter.
  71. *
  72. * @param string item The parameter whose value is required
  73. */
  74. var param = function( item )
  75. {
  76. if ( jQuery.isEmptyObject(parsed) )
  77. {
  78. setUp(); // if the URI has not been parsed yet then do this first...
  79. }
  80. return ( parsed.queryKey[item] === null ) ? null : parsed.queryKey[item];
  81. };
  82. /**
  83. * 'Constructor' (not really!) function.
  84. * Called whenever the URI changes to kick off re-parsing of the URI and splitting it up into segments.
  85. */
  86. var setUp = function()
  87. {
  88. parsed = parseUri();
  89. getSegments();
  90. };
  91. /**
  92. * Splits up the body of the URI into segments (i.e. sections delimited by '/')
  93. */
  94. var getSegments = function()
  95. {
  96. var p = parsed.path;
  97. segments = []; // clear out segments array
  98. segments = parsed.path.length == 1 ? {} : ( p.charAt( p.length - 1 ) == "/" ? p.substring( 1, p.length - 1 ) : path = p.substring( 1 ) ).split("/");
  99. };
  100. return {
  101. /**
  102. * Sets the parsing mode - either strict or loose. Set to loose by default.
  103. *
  104. * @param string mode The mode to set the parser to. Anything apart from a value of 'strict' will set it to loose!
  105. */
  106. setMode : function( mode )
  107. {
  108. options.strictMode = mode == "strict" ? true : false;
  109. return this;
  110. },
  111. /**
  112. * Sets URI to parse if you don't want to to parse the current page's URI.
  113. * Calling the function with no value for newUri resets it to the current page's URI.
  114. *
  115. * @param string newUri The URI to parse.
  116. */
  117. setUrl : function( newUri )
  118. {
  119. options.url = newUri === undefined ? window.location : newUri;
  120. setUp();
  121. return this;
  122. },
  123. /**
  124. * Returns the value of the specified URI segment. Segments are numbered from 1 to the number of segments.
  125. * For example the URI http://test.com/about/company/ segment(1) would return 'about'.
  126. *
  127. * If no integer is passed into the function it returns the number of segments in the URI.
  128. *
  129. * @param int pos The position of the segment to return. Can be empty.
  130. */
  131. segment : function( pos )
  132. {
  133. if ( jQuery.isEmptyObject(parsed) )
  134. {
  135. setUp(); // if the URI has not been parsed yet then do this first...
  136. }
  137. if ( pos === undefined )
  138. {
  139. return segments.length;
  140. }
  141. return ( segments[pos] === "" || segments[pos] === undefined ) ? null : segments[pos];
  142. },
  143. attr : key, // provides public access to private 'key' function - see above
  144. param : param // provides public access to private 'param' function - see above
  145. };
  146. }();