menu.js 981 B

12345678910111213141516171819202122232425262728293031323334353637
  1. (function($) {
  2. var REFRESH_INTERVAL = 100,
  3. url = location.hash.replace(/^#/, ''),
  4. load_content = function(url) {
  5. // Menu active class
  6. $('#menu a[href=#' + url + ']').addClass('active')
  7. .siblings('.active').removeClass('active');
  8. // AJAX call for content
  9. $('#content').load(url);
  10. },
  11. refresh_url = function(new_url) {
  12. if( new_url != url ) {
  13. url = new_url;
  14. load_content(url);
  15. }
  16. },
  17. // Compare the current hash to the url in a short interval
  18. // to allow browsing with forward/backward links
  19. interval,
  20. reset_interval = function() {
  21. clearInterval(interval);
  22. interval = setInterval(function() {
  23. refresh_url(location.hash.replace(/^#/, ''));
  24. }, REFRESH_INTERVAL);
  25. };
  26. // Default selected link
  27. if( url == '' )
  28. url = location.hash = 'pages';
  29. // Assert instant response to hashtag links
  30. $('a[href^=#]').live('click', function() {
  31. reset_interval();
  32. refresh_url($(this).attr('href').substring(1));
  33. });
  34. })(jQuery);