Javascript 404错误,使用后退按钮导航单个页面:window.history.pushState和window.onpopstate
我目前正在使用Html5的window.history.pushState和window.onpopstate劫持浏览器的后退按钮,以便在一个名为_ShoppingCart.cshtml的html页面上对内容进行前后浏览器导航。该网站正在使用带有Mvc后端的umbraco。只要导航在_ShoppingCart.cshtml(其中实现了window.history.pushState和window.onpopstate)上进行,一切都正常工作;但是,如果用户导航离开_ShoppingCart.cshtml页面,并尝试使用后退或前进按钮导航到使用window.history.pushState添加的URL之一(即购物车/配送或购物车/查看页面),则会抛出404(未找到页面),因为没有实际的购物车/配送或购物车/查看页面 我想以某种方式将cart/shipping或cart/reviewPay URL重定向到_ShoppingCart.cshtml。有什么办法可以这样做吗?谢谢你的帮助 我以此为例:Javascript 404错误,使用后退按钮导航单个页面:window.history.pushState和window.onpopstate,javascript,c#,html,model-view-controller,umbraco,Javascript,C#,Html,Model View Controller,Umbraco,我目前正在使用Html5的window.history.pushState和window.onpopstate劫持浏览器的后退按钮,以便在一个名为_ShoppingCart.cshtml的html页面上对内容进行前后浏览器导航。该网站正在使用带有Mvc后端的umbraco。只要导航在_ShoppingCart.cshtml(其中实现了window.history.pushState和window.onpopstate)上进行,一切都正常工作;但是,如果用户导航离开_ShoppingCart.cs
var setCurrentPage = function(url) {
$('h2 span').html(url || "/");
$("#menu-nav a[href='" + url + "']").fadeTo(500, 0.3);
};
$('#menu-nav a').click(function(e){
e.preventDefault();
var targetUrl = $(this).attr('href'),
targetTitle = $(this).attr('title');
$("#menu-nav a[href='" + window.location.pathname + "']").fadeTo(500, 1.0);
window.history.pushState({url: "" + targetUrl + ""}, targetTitle, targetUrl);
setCurrentPage(targetUrl);
});
window.onpopstate = function(e) {
$("#menu-nav a").fadeTo('fast', 1.0);
console.log(e);
setCurrentPage(e.state ? e.state.url : null);
};
您是否在应用程序中使用/cart/shipping/url(例如,它是否调用该路径上的任何内容),而不是用于显示目的?否,它仅用于显示目的。没有实际的页面。