Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. $(window).on('hashchange', function(e){
  2. $('body').
  3. });
  4.  
  5. function updateBodyClasses() {
  6.  
  7. var classNames = [];
  8.  
  9. // Use `.split()` to separate each key/value pair in the query by `&`
  10. window.location.search.split('&')
  11. // and loop over the results
  12. .forEach(function(c) {
  13.  
  14. // Now split each pair by '='
  15. var pair = c.split['='];
  16.  
  17. // Add the value to the left of the '='
  18. if (pair.length > 0) {
  19. classNames.push(pair[0]);
  20.  
  21. // if there are values on the right of the '='...
  22. if (pair.length > 1) {
  23.  
  24. // ... split them by ',' and loop through them
  25. pair[1].split(',').forEach(function(t) {
  26. classNames.push(t);
  27. });
  28. }
  29. }
  30. });
  31.  
  32. // Now append those classNames to the body
  33. $('body').addClass(classNames.join(' '));
  34. }
  35.  
  36. // If your ajax plugin modifies the url via HTML5 history api...
  37. $(window).on('popstate', updateBodyClasses);
  38.  
  39.  
  40. // Update classes on page load
  41. $(window).on('load', updateBodyClasses);
  42.  
  43. // If the ajax plugin modifies the url using window.location.replace()
  44. // you'll need to check on an interval whether things have changed
  45. // and update accordingly. the following example does this every 1000 milliseconds (or every second)
  46.  
  47. setInterval(updateBodyClasses, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement