$(function(){ var replacePage = function(href, onFinish){ $.get(href,{},function(raw){ var data = raw.substring(raw.indexOf('</head>')+7,raw.indexOf('</body>')+7); $('body').html(data); if(onFinish!=undefined)onFinish(); }); }; $("body").click(function(){ var $target = $(event.target); var href = $target.attr('href'); if(href=="")return; event.preventDefault(); console.log('SPA push',href); replacePage(href, function(){ history.pushState(null,null,href); }); }); window.addEventListener('popstate',function(event){ console.log('SPA pop'); replacePage(window.location.href); }); });