在不更改URL的情况下推送到历史记录并设置数据时:window.history.pushState({stateName:"myStateName",randomData:window.Math.random()},"myStateName",location.href);....然后监听弹出事件并通过按下浏览器中的返回按钮触发它:window.onpopstate=function(event){console.log(event.state);//logsnull}大多数时候你会得到null作为状态值而不是:{stateName:"myStateName",randomData:0
我试图阻止用户返回我的网络应用程序。为此,我尝试捕捉window.onpopstate并添加e.preventDefault以取消后退按钮效果。但这似乎并没有发生。window.addEventListener('popstate',function(e){console.log(e);e.preventDefault();});难道不能阻止浏览器的popstate事件吗?还是我做错了什么? 最佳答案 根据这个documentation,popstate事件是不可取消的:Specification:HTML5Interface:Po
首先,我不完全确定我正在做的或期望的是正确的。似乎没有太多关于此的文档,但我读过的内容表明这应该有效。我在尝试使用history.pushState时遇到了一些问题,所以我最终制作了一个演示页面以查看出了什么问题。UntitledDocumentwindow.onpopstate=function(){vard=newDate;console.log(d.toString());console.log(document.location.href);};$(document).ready(function(){$('#push').click(function(){varrand=Ma
首先,我不完全确定我正在做的或期望的是正确的。似乎没有太多关于此的文档,但我读过的内容表明这应该有效。我在尝试使用history.pushState时遇到了一些问题,所以我最终制作了一个演示页面以查看出了什么问题。UntitledDocumentwindow.onpopstate=function(){vard=newDate;console.log(d.toString());console.log(document.location.href);};$(document).ready(function(){$('#push').click(function(){varrand=Ma
我正在使用新的HTML5onpopstate事件。使用Firefox4,window.onpopstate事件在页面加载时触发,而在Webkit中,情况似乎并非如此。哪个是正确的行为? 最佳答案 Thepopstateeventisfiredincertaincaseswhennavigatingtoasessionhistoryentry.http://www.whatwg.org/specs/web-apps/current-work/#event-popstate根据我的理解,虽然我可能是错的,但加载页面确实意味着历史记录被创
我正在使用新的HTML5onpopstate事件。使用Firefox4,window.onpopstate事件在页面加载时触发,而在Webkit中,情况似乎并非如此。哪个是正确的行为? 最佳答案 Thepopstateeventisfiredincertaincaseswhennavigatingtoasessionhistoryentry.http://www.whatwg.org/specs/web-apps/current-work/#event-popstate根据我的理解,虽然我可能是错的,但加载页面确实意味着历史记录被创
我正在使用AJAX刷新一些页面,因此使用以下代码更新历史记录-/**Updatethepagehistory*/varpushState_object={ajax_string:ajax_string,security:security,};window.history.pushState(pushState_object,title,permalink);然后我在页面加载时调用这个onPopState函数-window.onpopstate=function(e){if(window.history.state!==null){initiate_load_updated_page(w
我正在使用AJAX刷新一些页面,因此使用以下代码更新历史记录-/**Updatethepagehistory*/varpushState_object={ajax_string:ajax_string,security:security,};window.history.pushState(pushState_object,title,permalink);然后我在页面加载时调用这个onPopState函数-window.onpopstate=function(e){if(window.history.state!==null){initiate_load_updated_page(w
通过窗口的卸载事件,可以向用户显示一个确认对话框,比方说,在有一个正在等待完成的请求的情况下,离开该页面将终止该请求。有没有办法用HTML5historyAPI的onpopstate来完成这个?或者任何其他具有相同结果的方式? 最佳答案 我想您可以修改pushState的行为以在推送新状态之前请求确认://StoreoriginalpushStatevar_pushState=window.history.pushState;//Somebadglobalvariabletodetermineifconfirmationisneed
我正在使用pushState为我网站上的ajax内容创建有意义的url。我想在用户点击前进或后退按钮时刷新ajax内容。然而,我遇到了问题,因为chrome(正确地)在“历史遍历”(前进/后退按钮)和“结束”(页面加载)上暗示了onpopstate。我想创建一个onpopstate处理程序来区分页面加载和历史遍历,因为如果它已经成功加载,我不想刷新我的ajax内容。谁能帮我区分这两者?window.onpopstate=function(event){//If:Forward/backbuttonpressed//Reloadanyajaxcontentwithnewvariables