function navigate(view) { history.pushState({view: view}, null, `#${view}`); loadView(view); } $(window).on('popstate', function(event) { const state = event.originalEvent.state; if (state && state.view) { loadView(state.view); } }); $(document).ready(function() { const initialView = location.hash.replace('#', '') || 'home'; loadView(initialView); });