Posted By

devnull69 on 02/16/12


Tagged

hack browser hash history back back-button


Versions (?)

Hash history hack


 / Published in: JavaScript
 

Include the following code into the global scope of your page. It will make sure that by navigating using the browser back/forward buttons you will see the correct dynamic content. It will also make the dynamic content bookmarkable.

You'll have to change getContent() according to what content you want to load depending on the "descr" paramter describing the content. descr will be the text that will be added to the URL as hash

  1. var currHash = "#";
  2.  
  3. window.onload = function() {
  4. checkHash();
  5. };
  6.  
  7. function getContent(descr) {
  8. switch(descr) {
  9. case "inhalt1" :
  10. document.getElementById('content').innerHTML = "<h2>Inhalt 1</h2>";
  11. break;
  12. case "inhalt2" :
  13. document.getElementById('content').innerHTML = "<h2>Inhalt 2</h2>";
  14. default: break;
  15. }
  16. window.location.hash = descr;
  17. currHash = "#" + descr;
  18. }
  19.  
  20. setInterval(checkHash, 500);
  21.  
  22. function checkHash() {
  23. if(window.location.hash && window.location.hash != currHash) {
  24. currHash = window.location.hash;
  25. getContent(window.location.hash.substring(1));
  26. }
  27. }

Report this snippet  

You need to login to post a comment.