Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 1st, 2012  |  syntax: None  |  size: 3.22 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (function ($) {
  2.     /**
  3.      * jQuery extension to parse hash (#) in url,
  4.      * fill in values of input boxes with the specific ID's,
  5.      * and automatically update the values on address bar by
  6.      * monitoring the change/keyup events.
  7.      */
  8.     $.fn.autohash = function () {
  9.        
  10.         // This extension is chainable
  11.         return this.each(function () {
  12.            
  13.             // Gets the window hash and get rid of the # in front of it
  14.             var hash = window.location.hash.substr(1, window.location.hash.length);
  15.             // Simple things, breaking it down into array
  16.             var array = hash.split("&");
  17.             // Making sure the element currently modified is cached
  18.             var element = $(this);
  19.  
  20.             // Keyup is the default event the extension fires on
  21.             element.bind('keyup', function () {
  22.                
  23.                 var hash2 = window.location.hash.substr(1, window.location.hash.length);
  24.                 var array2 = hash2.split("&");
  25.                 var result = Array();
  26.                 // Boolean to know whether the element is already found
  27.                 var set = false;
  28.  
  29.                 /**
  30.                  * Simple loop to go through each element in tbe hash array,
  31.                  * check if it's name matches the element's ID, if so
  32.                  * its value in the array is changed and then joined later,
  33.                  * unless the length is 0, in which case it can be removed from the hash
  34.                  */
  35.                 $.each(array2, function(i, value) {
  36.                     var split = value.split("=");
  37.  
  38.                     if (split[0] == element.attr('id')) {
  39.                         split[1] = element.val();
  40.                         set = true;
  41.                     }
  42.                     if (split[1] != undefined && split[1].length > 0) {
  43.                         result[result.length] = split.join("=");
  44.                     }
  45.                 });
  46.                
  47.                 // If element can't be found, it'll be added here
  48.                 if (!set && element.val().length) {
  49.                     result[result.length] = element.attr('id') + "=" + element.val();
  50.                 }
  51.  
  52.                 // Finally the address bar text is changed, with an IE hack, as per usual.
  53.                 if (result.join("&").length > 2)
  54.                     window.location.hash = result.join("&");
  55.                 else {
  56.                     if ("pushState" in history)
  57.                         history.pushState("", document.title, window.location.pathname);
  58.                     else
  59.                         window.location.hash = "";
  60.                 }
  61.             });
  62.  
  63.             // Bind the change event to the previous function
  64.             element.bind('change', function () {
  65.                 element.keyup();
  66.             });
  67.            
  68.             // Upon loading, the values from hash tag are set
  69.             // The change event is also triggered, if anything was listening to those
  70.             $.each(array, function (i, value) {
  71.                 var split = value.split("=");
  72.                
  73.                 if (split[0] == element.attr('id')) {
  74.                     element.val(split[1]);
  75.                     element.change();
  76.                 }
  77.             });
  78.         });
  79.     };
  80. })(jQuery);