$ (window) bind hashchange how to check hash hash?

I'm learning Google Ajax Crawlable

I am using the $(window) bind hashchange to control the loading of the ajax page.

my url is like: domain.com/#!/keywords&num=1

there are two kinds of changes

  • domain.com/#!/apple&num=1 => domain.com/#!/apple&num=2

  • domain.com/#!/apple&num=1 => domain.com/#!/banana&num=1

so how to check if the hash part of $(window) bind hashchange with apple => banana ? Thank you

 $(window).bind('hashchange', function() { // make a judge like if(){}else{} }); 
+6
source share
2 answers

Store the hash in a variable and update the variable at the end of the function. Consider:

 (function(){ var lastHash = location.hash; $(window).bind('hashchange', function() { var newHash = location.hash; // Do something var diff = compareHash(newHash, lastHash); alert("Difference between old and new hash:\n"+diff[0]+"\n\n"+dif[1]); //At the end of the func: lastHash = newHash; }); function compareHash(current, previous){ for(var i=0, len=Math.min(current.length, previous.length); i<len; i++){ if(current.charAt(0) != previous.charAt(0)) break; } current = current.substr(i); previous = previous.substr(i); for(var i=0, len=Math.min(current.length, previous.length); i<len; i++){ if(current.substr(-1) != previous.substr(-1)) break; } //Array: Current = New hash, previous = old hash return [current, previous]; } })() 
+13
source

I would store the original hash in a variable and then consider a new hash to judge the change. Once this is done, set the new hash as the original:

 var originalHash = window.location.hash; $(window).bind('hashchange', function() { var newHash = window.location.hash; //do your stuff based on the comparison of newHash to originalHash originalHash = newHash; }); 
+3
source

Source: https://habr.com/ru/post/898896/


All Articles