我做了一个单页的网站.当用户单击菜单按钮时,内容将加载ajax.它工作正常.为了改善SEO并允许用户复制/过去不同内容的URL,我使用
function show_content() {
// change URL in browser bar)
window.history.pushState("", "Content", "/content.php");
// ajax
$content.load("ajax/content.php?id="+id);
}
Run Code Online (Sandbox Code Playgroud)
它工作正常.URL更改,浏览器不会重新加载页面
但是,当用户单击浏览器中的后退按钮时,URL将更改并且必须加载内容.
我做到了这一点,它的工作原理:
window.onpopstate = function(event) {
if (document.location.pathname == '/4-content.php') {
show_content_1();
}
else if (document.location.pathname == '/1-content.php') {
show_content_2();
}
else if (document.location.pathname == '/6-content.php') {
show_content_();
}
};
Run Code Online (Sandbox Code Playgroud)
你知道是否有办法改进这段代码?