PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/assets/fbffa595/jquery.ba-bbq.js

https://gitlab.com/mrman/konvention
JavaScript | 1137 lines | 289 code | 126 blank | 722 comment | 61 complexity | ef0f941230274562550ba508d43ae4b7 MD5 | raw file
  1. /*!
  2. * jQuery BBQ: Back Button & Query Library - v1.2.1 - 2/17/2010
  3. * http://benalman.com/projects/jquery-bbq-plugin/
  4. *
  5. * Copyright (c) 2010 "Cowboy" Ben Alman
  6. * Dual licensed under the MIT and GPL licenses.
  7. * http://benalman.com/about/license/
  8. */
  9. // Script: jQuery BBQ: Back Button & Query Library
  10. //
  11. // *Version: 1.2.1, Last updated: 2/17/2010*
  12. //
  13. // Project Home - http://benalman.com/projects/jquery-bbq-plugin/
  14. // GitHub - http://github.com/cowboy/jquery-bbq/
  15. // Source - http://github.com/cowboy/jquery-bbq/raw/master/jquery.ba-bbq.js
  16. // (Minified) - http://github.com/cowboy/jquery-bbq/raw/master/jquery.ba-bbq.min.js (4.0kb)
  17. //
  18. // About: License
  19. //
  20. // Copyright (c) 2010 "Cowboy" Ben Alman,
  21. // Dual licensed under the MIT and GPL licenses.
  22. // http://benalman.com/about/license/
  23. //
  24. // About: Examples
  25. //
  26. // These working examples, complete with fully commented code, illustrate a few
  27. // ways in which this plugin can be used.
  28. //
  29. // Basic AJAX - http://benalman.com/code/projects/jquery-bbq/examples/fragment-basic/
  30. // Advanced AJAX - http://benalman.com/code/projects/jquery-bbq/examples/fragment-advanced/
  31. // jQuery UI Tabs - http://benalman.com/code/projects/jquery-bbq/examples/fragment-jquery-ui-tabs/
  32. // Deparam - http://benalman.com/code/projects/jquery-bbq/examples/deparam/
  33. //
  34. // About: Support and Testing
  35. //
  36. // Information about what version or versions of jQuery this plugin has been
  37. // tested with, what browsers it has been tested in, and where the unit tests
  38. // reside (so you can test it yourself).
  39. //
  40. // jQuery Versions - 1.3.2, 1.4.1, 1.4.2
  41. // Browsers Tested - Internet Explorer 6-8, Firefox 2-3.7, Safari 3-4,
  42. // Chrome 4-5, Opera 9.6-10.1.
  43. // Unit Tests - http://benalman.com/code/projects/jquery-bbq/unit/
  44. //
  45. // About: Release History
  46. //
  47. // 1.2.1 - (2/17/2010) Actually fixed the stale window.location Safari bug from
  48. // <jQuery hashchange event> in BBQ, which was the main reason for the
  49. // previous release!
  50. // 1.2 - (2/16/2010) Integrated <jQuery hashchange event> v1.2, which fixes a
  51. // Safari bug, the event can now be bound before DOM ready, and IE6/7
  52. // page should no longer scroll when the event is first bound. Also
  53. // added the <jQuery.param.fragment.noEscape> method, and reworked the
  54. // <hashchange event (BBQ)> internal "add" method to be compatible with
  55. // changes made to the jQuery 1.4.2 special events API.
  56. // 1.1.1 - (1/22/2010) Integrated <jQuery hashchange event> v1.1, which fixes an
  57. // obscure IE8 EmulateIE7 meta tag compatibility mode bug.
  58. // 1.1 - (1/9/2010) Broke out the jQuery BBQ event.special <hashchange event>
  59. // functionality into a separate plugin for users who want just the
  60. // basic event & back button support, without all the extra awesomeness
  61. // that BBQ provides. This plugin will be included as part of jQuery BBQ,
  62. // but also be available separately. See <jQuery hashchange event>
  63. // plugin for more information. Also added the <jQuery.bbq.removeState>
  64. // method and added additional <jQuery.deparam> examples.
  65. // 1.0.3 - (12/2/2009) Fixed an issue in IE 6 where location.search and
  66. // location.hash would report incorrectly if the hash contained the ?
  67. // character. Also <jQuery.param.querystring> and <jQuery.param.fragment>
  68. // will no longer parse params out of a URL that doesn't contain ? or #,
  69. // respectively.
  70. // 1.0.2 - (10/10/2009) Fixed an issue in IE 6/7 where the hidden IFRAME caused
  71. // a "This page contains both secure and nonsecure items." warning when
  72. // used on an https:// page.
  73. // 1.0.1 - (10/7/2009) Fixed an issue in IE 8. Since both "IE7" and "IE8
  74. // Compatibility View" modes erroneously report that the browser
  75. // supports the native window.onhashchange event, a slightly more
  76. // robust test needed to be added.
  77. // 1.0 - (10/2/2009) Initial release
  78. (function($,window){
  79. '$:nomunge'; // Used by YUI compressor.
  80. // Some convenient shortcuts.
  81. var undefined,
  82. aps = Array.prototype.slice,
  83. decode = decodeURIComponent,
  84. // Method / object references.
  85. jq_param = $.param,
  86. jq_param_fragment,
  87. jq_deparam,
  88. jq_deparam_fragment,
  89. jq_bbq = $.bbq = $.bbq || {},
  90. jq_bbq_pushState,
  91. jq_bbq_getState,
  92. jq_elemUrlAttr,
  93. jq_event_special = $.event.special,
  94. // Reused strings.
  95. str_hashchange = 'hashchange',
  96. str_querystring = 'querystring',
  97. str_fragment = 'fragment',
  98. str_elemUrlAttr = 'elemUrlAttr',
  99. str_location = 'location',
  100. str_href = 'href',
  101. str_src = 'src',
  102. // Reused RegExp.
  103. re_trim_querystring = /^.*\?|#.*$/g,
  104. re_trim_fragment = /^.*\#/,
  105. re_no_escape,
  106. // Used by jQuery.elemUrlAttr.
  107. elemUrlAttr_cache = {};
  108. // A few commonly used bits, broken out to help reduce minified file size.
  109. function is_string( arg ) {
  110. return typeof arg === 'string';
  111. };
  112. // Why write the same function twice? Let's curry! Mmmm, curry..
  113. function curry( func ) {
  114. var args = aps.call( arguments, 1 );
  115. return function() {
  116. return func.apply( this, args.concat( aps.call( arguments ) ) );
  117. };
  118. };
  119. // Get location.hash (or what you'd expect location.hash to be) sans any
  120. // leading #. Thanks for making this necessary, Firefox!
  121. function get_fragment( url ) {
  122. return url.replace( /^[^#]*#?(.*)$/, '$1' );
  123. };
  124. // Get location.search (or what you'd expect location.search to be) sans any
  125. // leading #. Thanks for making this necessary, IE6!
  126. function get_querystring( url ) {
  127. return url.replace( /(?:^[^?#]*\?([^#]*).*$)?.*/, '$1' );
  128. };
  129. // Section: Param (to string)
  130. //
  131. // Method: jQuery.param.querystring
  132. //
  133. // Retrieve the query string from a URL or if no arguments are passed, the
  134. // current window.location.
  135. //
  136. // Usage:
  137. //
  138. // > jQuery.param.querystring( [ url ] );
  139. //
  140. // Arguments:
  141. //
  142. // url - (String) A URL containing query string params to be parsed. If url
  143. // is not passed, the current window.location is used.
  144. //
  145. // Returns:
  146. //
  147. // (String) The parsed query string, with any leading "?" removed.
  148. //
  149. // Method: jQuery.param.querystring (build url)
  150. //
  151. // Merge a URL, with or without pre-existing query string params, plus any
  152. // object, params string or URL containing query string params into a new URL.
  153. //
  154. // Usage:
  155. //
  156. // > jQuery.param.querystring( url, params [, merge_mode ] );
  157. //
  158. // Arguments:
  159. //
  160. // url - (String) A valid URL for params to be merged into. This URL may
  161. // contain a query string and/or fragment (hash).
  162. // params - (String) A params string or URL containing query string params to
  163. // be merged into url.
  164. // params - (Object) A params object to be merged into url.
  165. // merge_mode - (Number) Merge behavior defaults to 0 if merge_mode is not
  166. // specified, and is as-follows:
  167. //
  168. // * 0: params in the params argument will override any query string
  169. // params in url.
  170. // * 1: any query string params in url will override params in the params
  171. // argument.
  172. // * 2: params argument will completely replace any query string in url.
  173. //
  174. // Returns:
  175. //
  176. // (String) Either a params string with urlencoded data or a URL with a
  177. // urlencoded query string in the format 'a=b&c=d&e=f'.
  178. // Method: jQuery.param.fragment
  179. //
  180. // Retrieve the fragment (hash) from a URL or if no arguments are passed, the
  181. // current window.location.
  182. //
  183. // Usage:
  184. //
  185. // > jQuery.param.fragment( [ url ] );
  186. //
  187. // Arguments:
  188. //
  189. // url - (String) A URL containing fragment (hash) params to be parsed. If
  190. // url is not passed, the current window.location is used.
  191. //
  192. // Returns:
  193. //
  194. // (String) The parsed fragment (hash) string, with any leading "#" removed.
  195. // Method: jQuery.param.fragment (build url)
  196. //
  197. // Merge a URL, with or without pre-existing fragment (hash) params, plus any
  198. // object, params string or URL containing fragment (hash) params into a new
  199. // URL.
  200. //
  201. // Usage:
  202. //
  203. // > jQuery.param.fragment( url, params [, merge_mode ] );
  204. //
  205. // Arguments:
  206. //
  207. // url - (String) A valid URL for params to be merged into. This URL may
  208. // contain a query string and/or fragment (hash).
  209. // params - (String) A params string or URL containing fragment (hash) params
  210. // to be merged into url.
  211. // params - (Object) A params object to be merged into url.
  212. // merge_mode - (Number) Merge behavior defaults to 0 if merge_mode is not
  213. // specified, and is as-follows:
  214. //
  215. // * 0: params in the params argument will override any fragment (hash)
  216. // params in url.
  217. // * 1: any fragment (hash) params in url will override params in the
  218. // params argument.
  219. // * 2: params argument will completely replace any query string in url.
  220. //
  221. // Returns:
  222. //
  223. // (String) Either a params string with urlencoded data or a URL with a
  224. // urlencoded fragment (hash) in the format 'a=b&c=d&e=f'.
  225. function jq_param_sub( is_fragment, get_func, url, params, merge_mode ) {
  226. var result,
  227. qs,
  228. matches,
  229. url_params,
  230. hash;
  231. if ( params !== undefined ) {
  232. // Build URL by merging params into url string.
  233. // matches[1] = url part that precedes params, not including trailing ?/#
  234. // matches[2] = params, not including leading ?/#
  235. // matches[3] = if in 'querystring' mode, hash including leading #, otherwise ''
  236. matches = url.match( is_fragment ? /^([^#]*)\#?(.*)$/ : /^([^#?]*)\??([^#]*)(#?.*)/ );
  237. // Get the hash if in 'querystring' mode, and it exists.
  238. hash = matches[3] || '';
  239. if ( merge_mode === 2 && is_string( params ) ) {
  240. // If merge_mode is 2 and params is a string, merge the fragment / query
  241. // string into the URL wholesale, without converting it into an object.
  242. qs = params.replace( is_fragment ? re_trim_fragment : re_trim_querystring, '' );
  243. } else {
  244. // Convert relevant params in url to object.
  245. url_params = jq_deparam( matches[2] );
  246. params = is_string( params )
  247. // Convert passed params string into object.
  248. ? jq_deparam[ is_fragment ? str_fragment : str_querystring ]( params )
  249. // Passed params object.
  250. : params;
  251. qs = merge_mode === 2 ? params // passed params replace url params
  252. : merge_mode === 1 ? $.extend( {}, params, url_params ) // url params override passed params
  253. : $.extend( {}, url_params, params ); // passed params override url params
  254. // Convert params object to a string.
  255. qs = jq_param( qs );
  256. // Unescape characters specified via $.param.noEscape. Since only hash-
  257. // history users have requested this feature, it's only enabled for
  258. // fragment-related params strings.
  259. if ( is_fragment ) {
  260. qs = qs.replace( re_no_escape, decode );
  261. }
  262. }
  263. // Build URL from the base url, querystring and hash. In 'querystring'
  264. // mode, ? is only added if a query string exists. In 'fragment' mode, #
  265. // is always added.
  266. result = matches[1] + ( is_fragment ? '#' : qs || !matches[1] ? '?' : '' ) + qs + hash;
  267. } else {
  268. // If URL was passed in, parse params from URL string, otherwise parse
  269. // params from window.location.
  270. result = get_func( url !== undefined ? url : window[ str_location ][ str_href ] );
  271. }
  272. return result;
  273. };
  274. jq_param[ str_querystring ] = curry( jq_param_sub, 0, get_querystring );
  275. jq_param[ str_fragment ] = jq_param_fragment = curry( jq_param_sub, 1, get_fragment );
  276. // Method: jQuery.param.fragment.noEscape
  277. //
  278. // Specify characters that will be left unescaped when fragments are created
  279. // or merged using <jQuery.param.fragment>, or when the fragment is modified
  280. // using <jQuery.bbq.pushState>. This option only applies to serialized data
  281. // object fragments, and not set-as-string fragments. Does not affect the
  282. // query string. Defaults to ",/" (comma, forward slash).
  283. //
  284. // Note that this is considered a purely aesthetic option, and will help to
  285. // create URLs that "look pretty" in the address bar or bookmarks, without
  286. // affecting functionality in any way. That being said, be careful to not
  287. // unescape characters that are used as delimiters or serve a special
  288. // purpose, such as the "#?&=+" (octothorpe, question mark, ampersand,
  289. // equals, plus) characters.
  290. //
  291. // Usage:
  292. //
  293. // > jQuery.param.fragment.noEscape( [ chars ] );
  294. //
  295. // Arguments:
  296. //
  297. // chars - (String) The characters to not escape in the fragment. If
  298. // unspecified, defaults to empty string (escape all characters).
  299. //
  300. // Returns:
  301. //
  302. // Nothing.
  303. jq_param_fragment.noEscape = function( chars ) {
  304. chars = chars || '';
  305. var arr = $.map( chars.split(''), encodeURIComponent );
  306. re_no_escape = new RegExp( arr.join('|'), 'g' );
  307. };
  308. // A sensible default. These are the characters people seem to complain about
  309. // "uglifying up the URL" the most.
  310. jq_param_fragment.noEscape( ',/' );
  311. // Section: Deparam (from string)
  312. //
  313. // Method: jQuery.deparam
  314. //
  315. // Deserialize a params string into an object, optionally coercing numbers,
  316. // booleans, null and undefined values; this method is the counterpart to the
  317. // internal jQuery.param method.
  318. //
  319. // Usage:
  320. //
  321. // > jQuery.deparam( params [, coerce ] );
  322. //
  323. // Arguments:
  324. //
  325. // params - (String) A params string to be parsed.
  326. // coerce - (Boolean) If true, coerces any numbers or true, false, null, and
  327. // undefined to their actual value. Defaults to false if omitted.
  328. //
  329. // Returns:
  330. //
  331. // (Object) An object representing the deserialized params string.
  332. $.deparam = jq_deparam = function( params, coerce ) {
  333. var obj = {},
  334. coerce_types = { 'true': !0, 'false': !1, 'null': null };
  335. // Iterate over all name=value pairs.
  336. $.each( params.replace( /\+/g, ' ' ).split( '&' ), function(j,v){
  337. var param = v.split( '=' ),
  338. key = decode( param[0] ),
  339. val,
  340. cur = obj,
  341. i = 0,
  342. // If key is more complex than 'foo', like 'a[]' or 'a[b][c]', split it
  343. // into its component parts.
  344. keys = key.split( '][' ),
  345. keys_last = keys.length - 1;
  346. // If the first keys part contains [ and the last ends with ], then []
  347. // are correctly balanced.
  348. if ( /\[/.test( keys[0] ) && /\]$/.test( keys[ keys_last ] ) ) {
  349. // Remove the trailing ] from the last keys part.
  350. keys[ keys_last ] = keys[ keys_last ].replace( /\]$/, '' );
  351. // Split first keys part into two parts on the [ and add them back onto
  352. // the beginning of the keys array.
  353. keys = keys.shift().split('[').concat( keys );
  354. keys_last = keys.length - 1;
  355. } else {
  356. // Basic 'foo' style key.
  357. keys_last = 0;
  358. }
  359. // Are we dealing with a name=value pair, or just a name?
  360. if ( param.length === 2 ) {
  361. val = decode( param[1] );
  362. // Coerce values.
  363. if ( coerce ) {
  364. val = val && !isNaN(val) ? +val // number
  365. : val === 'undefined' ? undefined // undefined
  366. : coerce_types[val] !== undefined ? coerce_types[val] // true, false, null
  367. : val; // string
  368. }
  369. if ( keys_last ) {
  370. // Complex key, build deep object structure based on a few rules:
  371. // * The 'cur' pointer starts at the object top-level.
  372. // * [] = array push (n is set to array length), [n] = array if n is
  373. // numeric, otherwise object.
  374. // * If at the last keys part, set the value.
  375. // * For each keys part, if the current level is undefined create an
  376. // object or array based on the type of the next keys part.
  377. // * Move the 'cur' pointer to the next level.
  378. // * Rinse & repeat.
  379. for ( ; i <= keys_last; i++ ) {
  380. key = keys[i] === '' ? cur.length : keys[i];
  381. cur = cur[key] = i < keys_last
  382. ? cur[key] || ( keys[i+1] && isNaN( keys[i+1] ) ? {} : [] )
  383. : val;
  384. }
  385. } else {
  386. // Simple key, even simpler rules, since only scalars and shallow
  387. // arrays are allowed.
  388. if ( $.isArray( obj[key] ) ) {
  389. // val is already an array, so push on the next value.
  390. obj[key].push( val );
  391. } else if ( obj[key] !== undefined ) {
  392. // val isn't an array, but since a second value has been specified,
  393. // convert val into an array.
  394. obj[key] = [ obj[key], val ];
  395. } else {
  396. // val is a scalar.
  397. obj[key] = val;
  398. }
  399. }
  400. } else if ( key ) {
  401. // No value was defined, so set something meaningful.
  402. obj[key] = coerce
  403. ? undefined
  404. : '';
  405. }
  406. });
  407. return obj;
  408. };
  409. // Method: jQuery.deparam.querystring
  410. //
  411. // Parse the query string from a URL or the current window.location,
  412. // deserializing it into an object, optionally coercing numbers, booleans,
  413. // null and undefined values.
  414. //
  415. // Usage:
  416. //
  417. // > jQuery.deparam.querystring( [ url ] [, coerce ] );
  418. //
  419. // Arguments:
  420. //
  421. // url - (String) An optional params string or URL containing query string
  422. // params to be parsed. If url is omitted, the current window.location
  423. // is used.
  424. // coerce - (Boolean) If true, coerces any numbers or true, false, null, and
  425. // undefined to their actual value. Defaults to false if omitted.
  426. //
  427. // Returns:
  428. //
  429. // (Object) An object representing the deserialized params string.
  430. // Method: jQuery.deparam.fragment
  431. //
  432. // Parse the fragment (hash) from a URL or the current window.location,
  433. // deserializing it into an object, optionally coercing numbers, booleans,
  434. // null and undefined values.
  435. //
  436. // Usage:
  437. //
  438. // > jQuery.deparam.fragment( [ url ] [, coerce ] );
  439. //
  440. // Arguments:
  441. //
  442. // url - (String) An optional params string or URL containing fragment (hash)
  443. // params to be parsed. If url is omitted, the current window.location
  444. // is used.
  445. // coerce - (Boolean) If true, coerces any numbers or true, false, null, and
  446. // undefined to their actual value. Defaults to false if omitted.
  447. //
  448. // Returns:
  449. //
  450. // (Object) An object representing the deserialized params string.
  451. function jq_deparam_sub( is_fragment, url_or_params, coerce ) {
  452. if ( url_or_params === undefined || typeof url_or_params === 'boolean' ) {
  453. // url_or_params not specified.
  454. coerce = url_or_params;
  455. url_or_params = jq_param[ is_fragment ? str_fragment : str_querystring ]();
  456. } else {
  457. url_or_params = is_string( url_or_params )
  458. ? url_or_params.replace( is_fragment ? re_trim_fragment : re_trim_querystring, '' )
  459. : url_or_params;
  460. }
  461. return jq_deparam( url_or_params, coerce );
  462. };
  463. jq_deparam[ str_querystring ] = curry( jq_deparam_sub, 0 );
  464. jq_deparam[ str_fragment ] = jq_deparam_fragment = curry( jq_deparam_sub, 1 );
  465. // Section: Element manipulation
  466. //
  467. // Method: jQuery.elemUrlAttr
  468. //
  469. // Get the internal "Default URL attribute per tag" list, or augment the list
  470. // with additional tag-attribute pairs, in case the defaults are insufficient.
  471. //
  472. // In the <jQuery.fn.querystring> and <jQuery.fn.fragment> methods, this list
  473. // is used to determine which attribute contains the URL to be modified, if
  474. // an "attr" param is not specified.
  475. //
  476. // Default Tag-Attribute List:
  477. //
  478. // a - href
  479. // base - href
  480. // iframe - src
  481. // img - src
  482. // input - src
  483. // form - action
  484. // link - href
  485. // script - src
  486. //
  487. // Usage:
  488. //
  489. // > jQuery.elemUrlAttr( [ tag_attr ] );
  490. //
  491. // Arguments:
  492. //
  493. // tag_attr - (Object) An object containing a list of tag names and their
  494. // associated default attribute names in the format { tag: 'attr', ... } to
  495. // be merged into the internal tag-attribute list.
  496. //
  497. // Returns:
  498. //
  499. // (Object) An object containing all stored tag-attribute values.
  500. // Only define function and set defaults if function doesn't already exist, as
  501. // the urlInternal plugin will provide this method as well.
  502. $[ str_elemUrlAttr ] || ($[ str_elemUrlAttr ] = function( obj ) {
  503. return $.extend( elemUrlAttr_cache, obj );
  504. })({
  505. a: str_href,
  506. base: str_href,
  507. iframe: str_src,
  508. img: str_src,
  509. input: str_src,
  510. form: 'action',
  511. link: str_href,
  512. script: str_src
  513. });
  514. jq_elemUrlAttr = $[ str_elemUrlAttr ];
  515. // Method: jQuery.fn.querystring
  516. //
  517. // Update URL attribute in one or more elements, merging the current URL (with
  518. // or without pre-existing query string params) plus any params object or
  519. // string into a new URL, which is then set into that attribute. Like
  520. // <jQuery.param.querystring (build url)>, but for all elements in a jQuery
  521. // collection.
  522. //
  523. // Usage:
  524. //
  525. // > jQuery('selector').querystring( [ attr, ] params [, merge_mode ] );
  526. //
  527. // Arguments:
  528. //
  529. // attr - (String) Optional name of an attribute that will contain a URL to
  530. // merge params or url into. See <jQuery.elemUrlAttr> for a list of default
  531. // attributes.
  532. // params - (Object) A params object to be merged into the URL attribute.
  533. // params - (String) A URL containing query string params, or params string
  534. // to be merged into the URL attribute.
  535. // merge_mode - (Number) Merge behavior defaults to 0 if merge_mode is not
  536. // specified, and is as-follows:
  537. //
  538. // * 0: params in the params argument will override any params in attr URL.
  539. // * 1: any params in attr URL will override params in the params argument.
  540. // * 2: params argument will completely replace any query string in attr
  541. // URL.
  542. //
  543. // Returns:
  544. //
  545. // (jQuery) The initial jQuery collection of elements, but with modified URL
  546. // attribute values.
  547. // Method: jQuery.fn.fragment
  548. //
  549. // Update URL attribute in one or more elements, merging the current URL (with
  550. // or without pre-existing fragment/hash params) plus any params object or
  551. // string into a new URL, which is then set into that attribute. Like
  552. // <jQuery.param.fragment (build url)>, but for all elements in a jQuery
  553. // collection.
  554. //
  555. // Usage:
  556. //
  557. // > jQuery('selector').fragment( [ attr, ] params [, merge_mode ] );
  558. //
  559. // Arguments:
  560. //
  561. // attr - (String) Optional name of an attribute that will contain a URL to
  562. // merge params into. See <jQuery.elemUrlAttr> for a list of default
  563. // attributes.
  564. // params - (Object) A params object to be merged into the URL attribute.
  565. // params - (String) A URL containing fragment (hash) params, or params
  566. // string to be merged into the URL attribute.
  567. // merge_mode - (Number) Merge behavior defaults to 0 if merge_mode is not
  568. // specified, and is as-follows:
  569. //
  570. // * 0: params in the params argument will override any params in attr URL.
  571. // * 1: any params in attr URL will override params in the params argument.
  572. // * 2: params argument will completely replace any fragment (hash) in attr
  573. // URL.
  574. //
  575. // Returns:
  576. //
  577. // (jQuery) The initial jQuery collection of elements, but with modified URL
  578. // attribute values.
  579. function jq_fn_sub( mode, force_attr, params, merge_mode ) {
  580. if ( !is_string( params ) && typeof params !== 'object' ) {
  581. // force_attr not specified.
  582. merge_mode = params;
  583. params = force_attr;
  584. force_attr = undefined;
  585. }
  586. return this.each(function(){
  587. var that = $(this),
  588. // Get attribute specified, or default specified via $.elemUrlAttr.
  589. attr = force_attr || jq_elemUrlAttr()[ ( this.nodeName || '' ).toLowerCase() ] || '',
  590. // Get URL value.
  591. url = attr && that.attr( attr ) || '';
  592. // Update attribute with new URL.
  593. that.attr( attr, jq_param[ mode ]( url, params, merge_mode ) );
  594. });
  595. };
  596. $.fn[ str_querystring ] = curry( jq_fn_sub, str_querystring );
  597. $.fn[ str_fragment ] = curry( jq_fn_sub, str_fragment );
  598. // Section: History, hashchange event
  599. //
  600. // Method: jQuery.bbq.pushState
  601. //
  602. // Adds a 'state' into the browser history at the current position, setting
  603. // location.hash and triggering any bound <hashchange event> callbacks
  604. // (provided the new state is different than the previous state).
  605. //
  606. // If no arguments are passed, an empty state is created, which is just a
  607. // shortcut for jQuery.bbq.pushState( {}, 2 ).
  608. //
  609. // Usage:
  610. //
  611. // > jQuery.bbq.pushState( [ params [, merge_mode ] ] );
  612. //
  613. // Arguments:
  614. //
  615. // params - (String) A serialized params string or a hash string beginning
  616. // with # to merge into location.hash.
  617. // params - (Object) A params object to merge into location.hash.
  618. // merge_mode - (Number) Merge behavior defaults to 0 if merge_mode is not
  619. // specified (unless a hash string beginning with # is specified, in which
  620. // case merge behavior defaults to 2), and is as-follows:
  621. //
  622. // * 0: params in the params argument will override any params in the
  623. // current state.
  624. // * 1: any params in the current state will override params in the params
  625. // argument.
  626. // * 2: params argument will completely replace current state.
  627. //
  628. // Returns:
  629. //
  630. // Nothing.
  631. //
  632. // Additional Notes:
  633. //
  634. // * Setting an empty state may cause the browser to scroll.
  635. // * Unlike the fragment and querystring methods, if a hash string beginning
  636. // with # is specified as the params agrument, merge_mode defaults to 2.
  637. jq_bbq.pushState = jq_bbq_pushState = function( params, merge_mode ) {
  638. if ( is_string( params ) && /^#/.test( params ) && merge_mode === undefined ) {
  639. // Params string begins with # and merge_mode not specified, so completely
  640. // overwrite window.location.hash.
  641. merge_mode = 2;
  642. }
  643. var has_args = params !== undefined,
  644. // Merge params into window.location using $.param.fragment.
  645. url = jq_param_fragment( window[ str_location ][ str_href ],
  646. has_args ? params : {}, has_args ? merge_mode : 2 );
  647. // Set new window.location.href. If hash is empty, use just # to prevent
  648. // browser from reloading the page. Note that Safari 3 & Chrome barf on
  649. // location.hash = '#'.
  650. window[ str_location ][ str_href ] = url + ( /#/.test( url ) ? '' : '#' );
  651. };
  652. // Method: jQuery.bbq.getState
  653. //
  654. // Retrieves the current 'state' from the browser history, parsing
  655. // location.hash for a specific key or returning an object containing the
  656. // entire state, optionally coercing numbers, booleans, null and undefined
  657. // values.
  658. //
  659. // Usage:
  660. //
  661. // > jQuery.bbq.getState( [ key ] [, coerce ] );
  662. //
  663. // Arguments:
  664. //
  665. // key - (String) An optional state key for which to return a value.
  666. // coerce - (Boolean) If true, coerces any numbers or true, false, null, and
  667. // undefined to their actual value. Defaults to false.
  668. //
  669. // Returns:
  670. //
  671. // (Anything) If key is passed, returns the value corresponding with that key
  672. // in the location.hash 'state', or undefined. If not, an object
  673. // representing the entire 'state' is returned.
  674. jq_bbq.getState = jq_bbq_getState = function( key, coerce ) {
  675. return key === undefined || typeof key === 'boolean'
  676. ? jq_deparam_fragment( key ) // 'key' really means 'coerce' here
  677. : jq_deparam_fragment( coerce )[ key ];
  678. };
  679. // Method: jQuery.bbq.removeState
  680. //
  681. // Remove one or more keys from the current browser history 'state', creating
  682. // a new state, setting location.hash and triggering any bound
  683. // <hashchange event> callbacks (provided the new state is different than
  684. // the previous state).
  685. //
  686. // If no arguments are passed, an empty state is created, which is just a
  687. // shortcut for jQuery.bbq.pushState( {}, 2 ).
  688. //
  689. // Usage:
  690. //
  691. // > jQuery.bbq.removeState( [ key [, key ... ] ] );
  692. //
  693. // Arguments:
  694. //
  695. // key - (String) One or more key values to remove from the current state,
  696. // passed as individual arguments.
  697. // key - (Array) A single array argument that contains a list of key values
  698. // to remove from the current state.
  699. //
  700. // Returns:
  701. //
  702. // Nothing.
  703. //
  704. // Additional Notes:
  705. //
  706. // * Setting an empty state may cause the browser to scroll.
  707. jq_bbq.removeState = function( arr ) {
  708. var state = {};
  709. // If one or more arguments is passed..
  710. if ( arr !== undefined ) {
  711. // Get the current state.
  712. state = jq_bbq_getState();
  713. // For each passed key, delete the corresponding property from the current
  714. // state.
  715. $.each( $.isArray( arr ) ? arr : arguments, function(i,v){
  716. delete state[ v ];
  717. });
  718. }
  719. // Set the state, completely overriding any existing state.
  720. jq_bbq_pushState( state, 2 );
  721. };
  722. // Event: hashchange event (BBQ)
  723. //
  724. // Usage in jQuery 1.4 and newer:
  725. //
  726. // In jQuery 1.4 and newer, the event object passed into any hashchange event
  727. // callback is augmented with a copy of the location.hash fragment at the time
  728. // the event was triggered as its event.fragment property. In addition, the
  729. // event.getState method operates on this property (instead of location.hash)
  730. // which allows this fragment-as-a-state to be referenced later, even after
  731. // window.location may have changed.
  732. //
  733. // Note that event.fragment and event.getState are not defined according to
  734. // W3C (or any other) specification, but will still be available whether or
  735. // not the hashchange event exists natively in the browser, because of the
  736. // utility they provide.
  737. //
  738. // The event.fragment property contains the output of <jQuery.param.fragment>
  739. // and the event.getState method is equivalent to the <jQuery.bbq.getState>
  740. // method.
  741. //
  742. // > $(window).bind( 'hashchange', function( event ) {
  743. // > var hash_str = event.fragment,
  744. // > param_obj = event.getState(),
  745. // > param_val = event.getState( 'param_name' ),
  746. // > param_val_coerced = event.getState( 'param_name', true );
  747. // > ...
  748. // > });
  749. //
  750. // Usage in jQuery 1.3.2:
  751. //
  752. // In jQuery 1.3.2, the event object cannot to be augmented as in jQuery 1.4+,
  753. // so the fragment state isn't bound to the event object and must instead be
  754. // parsed using the <jQuery.param.fragment> and <jQuery.bbq.getState> methods.
  755. //
  756. // > $(window).bind( 'hashchange', function( event ) {
  757. // > var hash_str = $.param.fragment(),
  758. // > param_obj = $.bbq.getState(),
  759. // > param_val = $.bbq.getState( 'param_name' ),
  760. // > param_val_coerced = $.bbq.getState( 'param_name', true );
  761. // > ...
  762. // > });
  763. //
  764. // Additional Notes:
  765. //
  766. // * Due to changes in the special events API, jQuery BBQ v1.2 or newer is
  767. // required to enable the augmented event object in jQuery 1.4.2 and newer.
  768. // * See <jQuery hashchange event> for more detailed information.
  769. jq_event_special[ str_hashchange ] = $.extend( jq_event_special[ str_hashchange ], {
  770. // Augmenting the event object with the .fragment property and .getState
  771. // method requires jQuery 1.4 or newer. Note: with 1.3.2, everything will
  772. // work, but the event won't be augmented)
  773. add: function( handleObj ) {
  774. var old_handler;
  775. function new_handler(e) {
  776. // e.fragment is set to the value of location.hash (with any leading #
  777. // removed) at the time the event is triggered.
  778. var hash = e[ str_fragment ] = jq_param_fragment();
  779. // e.getState() works just like $.bbq.getState(), but uses the
  780. // e.fragment property stored on the event object.
  781. e.getState = function( key, coerce ) {
  782. return key === undefined || typeof key === 'boolean'
  783. ? jq_deparam( hash, key ) // 'key' really means 'coerce' here
  784. : jq_deparam( hash, coerce )[ key ];
  785. };
  786. old_handler.apply( this, arguments );
  787. };
  788. // This may seem a little complicated, but it normalizes the special event
  789. // .add method between jQuery 1.4/1.4.1 and 1.4.2+
  790. if ( $.isFunction( handleObj ) ) {
  791. // 1.4, 1.4.1
  792. old_handler = handleObj;
  793. return new_handler;
  794. } else {
  795. // 1.4.2+
  796. old_handler = handleObj.handler;
  797. handleObj.handler = new_handler;
  798. }
  799. }
  800. });
  801. })(jQuery,this);
  802. /*!
  803. * jQuery hashchange event - v1.2 - 2/11/2010
  804. * http://benalman.com/projects/jquery-hashchange-plugin/
  805. *
  806. * Copyright (c) 2010 "Cowboy" Ben Alman
  807. * Dual licensed under the MIT and GPL licenses.
  808. * http://benalman.com/about/license/
  809. */
  810. // Script: jQuery hashchange event
  811. //
  812. // *Version: 1.2, Last updated: 2/11/2010*
  813. //
  814. // Project Home - http://benalman.com/projects/jquery-hashchange-plugin/
  815. // GitHub - http://github.com/cowboy/jquery-hashchange/
  816. // Source - http://github.com/cowboy/jquery-hashchange/raw/master/jquery.ba-hashchange.js
  817. // (Minified) - http://github.com/cowboy/jquery-hashchange/raw/master/jquery.ba-hashchange.min.js (1.1kb)
  818. //
  819. // About: License
  820. //
  821. // Copyright (c) 2010 "Cowboy" Ben Alman,
  822. // Dual licensed under the MIT and GPL licenses.
  823. // http://benalman.com/about/license/
  824. //
  825. // About: Examples
  826. //
  827. // This working example, complete with fully commented code, illustrate one way
  828. // in which this plugin can be used.
  829. //
  830. // hashchange event - http://benalman.com/code/projects/jquery-hashchange/examples/hashchange/
  831. //
  832. // About: Support and Testing
  833. //
  834. // Information about what version or versions of jQuery this plugin has been
  835. // tested with, what browsers it has been tested in, and where the unit tests
  836. // reside (so you can test it yourself).
  837. //
  838. // jQuery Versions - 1.3.2, 1.4.1, 1.4.2
  839. // Browsers Tested - Internet Explorer 6-8, Firefox 2-3.7, Safari 3-4, Chrome, Opera 9.6-10.1.
  840. // Unit Tests - http://benalman.com/code/projects/jquery-hashchange/unit/
  841. //
  842. // About: Known issues
  843. //
  844. // While this jQuery hashchange event implementation is quite stable and robust,
  845. // there are a few unfortunate browser bugs surrounding expected hashchange
  846. // event-based behaviors, independent of any JavaScript window.onhashchange
  847. // abstraction. See the following examples for more information:
  848. //
  849. // Chrome: Back Button - http://benalman.com/code/projects/jquery-hashchange/examples/bug-chrome-back-button/
  850. // Firefox: Remote XMLHttpRequest - http://benalman.com/code/projects/jquery-hashchange/examples/bug-firefox-remote-xhr/
  851. // WebKit: Back Button in an Iframe - http://benalman.com/code/projects/jquery-hashchange/examples/bug-webkit-hash-iframe/
  852. // Safari: Back Button from a different domain - http://benalman.com/code/projects/jquery-hashchange/examples/bug-safari-back-from-diff-domain/
  853. //
  854. // About: Release History
  855. //
  856. // 1.2 - (2/11/2010) Fixed a bug where coming back to a page using this plugin
  857. // from a page on another domain would cause an error in Safari 4. Also,
  858. // IE6/7 Iframe is now inserted after the body (this actually works),
  859. // which prevents the page from scrolling when the event is first bound.
  860. // Event can also now be bound before DOM ready, but it won't be usable
  861. // before then in IE6/7.
  862. // 1.1 - (1/21/2010) Incorporated document.documentMode test to fix IE8 bug
  863. // where browser version is incorrectly reported as 8.0, despite
  864. // inclusion of the X-UA-Compatible IE=EmulateIE7 meta tag.
  865. // 1.0 - (1/9/2010) Initial Release. Broke out the jQuery BBQ event.special
  866. // window.onhashchange functionality into a separate plugin for users
  867. // who want just the basic event & back button support, without all the
  868. // extra awesomeness that BBQ provides. This plugin will be included as
  869. // part of jQuery BBQ, but also be available separately.
  870. (function($,window,undefined){
  871. '$:nomunge'; // Used by YUI compressor.
  872. // Method / object references.
  873. var fake_onhashchange,
  874. jq_event_special = $.event.special,
  875. // Reused strings.
  876. str_location = 'location',
  877. str_hashchange = 'hashchange',
  878. str_href = 'href',
  879. // IE6/7 specifically need some special love when it comes to back-button
  880. // support, so let's do a little browser sniffing..
  881. browser = $.browser,
  882. mode = document.documentMode,
  883. is_old_ie = browser.msie && ( mode === undefined || mode < 8 ),
  884. // Does the browser support window.onhashchange? Test for IE version, since
  885. // IE8 incorrectly reports this when in "IE7" or "IE8 Compatibility View"!
  886. supports_onhashchange = 'on' + str_hashchange in window && !is_old_ie;
  887. // Get location.hash (or what you'd expect location.hash to be) sans any
  888. // leading #. Thanks for making this necessary, Firefox!
  889. function get_fragment( url ) {
  890. url = url || window[ str_location ][ str_href ];
  891. return url.replace( /^[^#]*#?(.*)$/, '$1' );
  892. };
  893. // Property: jQuery.hashchangeDelay
  894. //
  895. // The numeric interval (in milliseconds) at which the <hashchange event>
  896. // polling loop executes. Defaults to 100.
  897. $[ str_hashchange + 'Delay' ] = 100;
  898. // Event: hashchange event
  899. //
  900. // Fired when location.hash changes. In browsers that support it, the native
  901. // window.onhashchange event is used (IE8, FF3.6), otherwise a polling loop is
  902. // initialized, running every <jQuery.hashchangeDelay> milliseconds to see if
  903. // the hash has changed. In IE 6 and 7, a hidden Iframe is created to allow
  904. // the back button and hash-based history to work.
  905. //
  906. // Usage:
  907. //
  908. // > $(window).bind( 'hashchange', function(e) {
  909. // > var hash = location.hash;
  910. // > ...
  911. // > });
  912. //
  913. // Additional Notes:
  914. //
  915. // * The polling loop and Iframe are not created until at least one callback
  916. // is actually bound to 'hashchange'.
  917. // * If you need the bound callback(s) to execute immediately, in cases where
  918. // the page 'state' exists on page load (via bookmark or page refresh, for
  919. // example) use $(window).trigger( 'hashchange' );
  920. // * The event can be bound before DOM ready, but since it won't be usable
  921. // before then in IE6/7 (due to the necessary Iframe), recommended usage is
  922. // to bind it inside a $(document).ready() callback.
  923. jq_event_special[ str_hashchange ] = $.extend( jq_event_special[ str_hashchange ], {
  924. // Called only when the first 'hashchange' event is bound to window.
  925. setup: function() {
  926. // If window.onhashchange is supported natively, there's nothing to do..
  927. if ( supports_onhashchange ) { return false; }
  928. // Otherwise, we need to create our own. And we don't want to call this
  929. // until the user binds to the event, just in case they never do, since it
  930. // will create a polling loop and possibly even a hidden Iframe.
  931. $( fake_onhashchange.start );
  932. },
  933. // Called only when the last 'hashchange' event is unbound from window.
  934. teardown: function() {
  935. // If window.onhashchange is supported natively, there's nothing to do..
  936. if ( supports_onhashchange ) { return false; }
  937. // Otherwise, we need to stop ours (if possible).
  938. $( fake_onhashchange.stop );
  939. }
  940. });
  941. // fake_onhashchange does all the work of triggering the window.onhashchange
  942. // event for browsers that don't natively support it, including creating a
  943. // polling loop to watch for hash changes and in IE 6/7 creating a hidden
  944. // Iframe to enable back and forward.
  945. fake_onhashchange = (function(){
  946. var self = {},
  947. timeout_id,
  948. iframe,
  949. set_history,
  950. get_history;
  951. // Initialize. In IE 6/7, creates a hidden Iframe for history handling.
  952. function init(){
  953. // Most browsers don't need special methods here..
  954. set_history = get_history = function(val){ return val; };
  955. // But IE6/7 do!
  956. if ( is_old_ie ) {
  957. // Create hidden Iframe after the end of the body to prevent initial
  958. // page load from scrolling unnecessarily.
  959. iframe = $('<iframe src="javascript:0"/>').hide().insertAfter( 'body' )[0].contentWindow;
  960. // Get history by looking at the hidden Iframe's location.hash.
  961. get_history = function() {
  962. return get_fragment( iframe.document[ str_location ][ str_href ] );
  963. };
  964. // Set a new history item by opening and then closing the Iframe
  965. // document, *then* setting its location.hash.
  966. set_history = function( hash, history_hash ) {
  967. if ( hash !== history_hash ) {
  968. var doc = iframe.document;
  969. doc.open().close();
  970. doc[ str_location ].hash = '#' + hash;
  971. }
  972. };
  973. // Set initial history.
  974. set_history( get_fragment() );
  975. }
  976. };
  977. // Start the polling loop.
  978. self.start = function() {
  979. // Polling loop is already running!
  980. if ( timeout_id ) { return; }
  981. // Remember the initial hash so it doesn't get triggered immediately.
  982. var last_hash = get_fragment();
  983. // Initialize if not yet initialized.
  984. set_history || init();
  985. // This polling loop checks every $.hashchangeDelay milliseconds to see if
  986. // location.hash has changed, and triggers the 'hashchange' event on
  987. // window when necessary.
  988. (function loopy(){
  989. var hash = get_fragment(),
  990. history_hash = get_history( last_hash );
  991. if ( hash !== last_hash ) {
  992. set_history( last_hash = hash, history_hash );
  993. $(window).trigger( str_hashchange );
  994. } else if ( history_hash !== last_hash ) {
  995. window[ str_location ][ str_href ] = window[ str_location ][ str_href ].replace( /#.*/, '' ) + '#' + history_hash;
  996. }
  997. timeout_id = setTimeout( loopy, $[ str_hashchange + 'Delay' ] );
  998. })();
  999. };
  1000. // Stop the polling loop, but only if an IE6/7 Iframe wasn't created. In
  1001. // that case, even if there are no longer any bound event handlers, the
  1002. // polling loop is still necessary for back/next to work at all!
  1003. self.stop = function() {
  1004. if ( !iframe ) {
  1005. timeout_id && clearTimeout( timeout_id );
  1006. timeout_id = 0;
  1007. }
  1008. };
  1009. return self;
  1010. })();
  1011. })(jQuery,this);