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

Untitled

By: a guest on May 31st, 2012  |  syntax: None  |  size: 1.24 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. $(window) bind hashchange how to check part hash changed?
  2. $(window).bind('hashchange', function() {
  3.     // make a judge like  if(){}else{}
  4. });
  5.        
  6. (function(){
  7.     var lastHash = location.hash;
  8.     $(window).bind('hashchange', function() {
  9.         var newHash = location.hash;
  10.         // Do something
  11.         var diff = compareHash(newHash, lastHash);
  12.         alert("Difference between old and new hash:n"+diff[0]+"nn"+dif[1]);
  13.  
  14.         //At the end of the func:
  15.         lastHash = newHash;
  16.     });
  17.  
  18.     function compareHash(current, previous){
  19.         for(var i=0, len=Math.min(current.length, previous.length); i<len; i++){
  20.             if(current.charAt(0) != previous.charAt(0)) break;
  21.         }
  22.         current = current.substr(i);
  23.         previous = previous.substr(i);
  24.         for(var i=0, len=Math.min(current.length, previous.length); i<len; i++){
  25.             if(current.substr(-1) != previous.substr(-1)) break;
  26.         }
  27.  
  28.         //Array: Current = New hash, previous = old hash
  29.         return [current, previous];
  30.     }
  31. })()
  32.        
  33. var originalHash = window.location.hash;
  34. $(window).bind('hashchange', function() {
  35.     var newHash = window.location.hash;
  36.     //do your stuff based on the comparison of newHash to originalHash
  37.  
  38.     originalHash = newHash;
  39. });