Advertisement
adnan360

Javascript PushState

Mar 1st, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     $(document).on("click","#flymenu li a", function() {
  2.         var url = $(this).attr('href');
  3.        
  4.         //loadContent(url,true);
  5.        
  6.         $.get(url,function(response){
  7.             flymenu = $("#flymenu",response).html();
  8.             $('#flymenu').html(flymenu);
  9.            
  10.             heading = $("#main_header",response).html();
  11.             //$('#flymenu').append(content);
  12.             $('#main_header').html(heading);
  13.            
  14.             // mytitle = $("head title",response).html();
  15.             $('title').html(heading);
  16.             // alert(mytitle);
  17.            
  18.             // keep the height of page when loading through ajax
  19.             $("#maincontent").css('height',maincontent_height);
  20.            
  21.             // product_slider = $(".product-slider",response).html();
  22.             product_slider = $(".product-slider",response).html();
  23.             $('.product-slider').html(product_slider);
  24.            
  25.             carousel = $("#bx-pager",response).html();
  26.             $('#bx-pager').html(carousel);
  27.            
  28.             //var stateObj = { foo: "bar" };
  29.             history.pushState('', heading, url);
  30.            
  31.             destroyslider();
  32.             prepareslider();
  33.         });
  34.        
  35.         return false;
  36.     });
  37.    
  38.     function loadContent(url,setState) {
  39.         setState = typeof setState !== 'undefined' ? setState : true;
  40.         $.get(url,function(response){
  41.             flymenu = $("#flymenu",response).html();
  42.             $('#flymenu').html(flymenu);
  43.            
  44.             heading = $("#main_header",response).html();
  45.             //$('#flymenu').append(content);
  46.             $('#main_header').html(heading);
  47.            
  48.             // mytitle = $("head title",response).html();
  49.             $('title').html(heading);
  50.             // alert(mytitle);
  51.            
  52.             // keep the height of page when loading through ajax
  53.             $("#maincontent").css('height',maincontent_height);
  54.            
  55.             // product_slider = $(".product-slider",response).html();
  56.             product_slider = $(".product-slider",response).html();
  57.             $('.product-slider').html(product_slider);
  58.            
  59.             carousel = $("#bx-pager",response).html();
  60.             $('#bx-pager').html(carousel);
  61.            
  62.             if (setState) {
  63.                 //var stateObj = { foo: "bar" };
  64.                 history.pushState('', heading, url);
  65.             }
  66.            
  67.             destroyslider();
  68.             prepareslider();
  69.         });
  70.     }
  71.    
  72.     // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
  73.     window.onpopstate = function(event) {
  74.         //$("#loading").show();
  75.         console.log("url changed: "+location.pathname);
  76.         loadContent(location.pathname,false);
  77.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement