简体   繁体   English

HTML5 history.pushState

[英]HTML5 history.pushState

I am using history.pushstate method to add the history into the browser and added the history as follows in the browser. 我正在使用history.pushstate方法将历史记录添加到浏览器中,并在浏览器中按如下所示添加历史记录。

       history.pushState("", "",myurl);

The history added in the browser correctly.(When the browser history clicked the correct page getting loaded.) 历史记录已正确添加到浏览器中。(当浏览器历史记录单击时,正确的页面将被加载。)

But when i click the back button in the browser it doesn't go back.. 但是,当我单击浏览器中的“后退”按钮时,它不会返回。

(ie I am using hashchange functionality to show different contents in the page). (即,我正在使用hashchange功能在页面中显示不同的内容)。

Actually it requires another click to go back.(ie two clicks of back button to go back to the previous page added in the history). 实际上,它需要再次单击才能返回。(即单击两次后退按钮可以返回到历史记录中添加的上一页)。

How can i make it to back in only one click of back button. 我怎样才能使其一键返回就可以返回。

Don't use hashchange for history; 不要将hashchange用于历史记录; that's only for the fragment changing. 仅用于片段更改。 Use popstate . 使用popstate

window.addEventListener('popstate', function () {

});

http://jsfiddle.net/ExplosionPIlls/NkEdg/ http://jsfiddle.net/ExplosionPIlls/NkEdg/

I found a working solution as follows. 我找到了一个可行的解决方案,如下所示。

Using replacestate instead of pushstate solved the problem. 使用replacestate而不是pushstate解决了该问题。

Pushstate add the history but it doesn't points the top of the stack.So at first click the same page from history loaded and in the next click the previous page get loaded. Pushstate会添加历史记录,但不会指向堆栈的顶部。因此,首先单击加载历史记录中的同一页面,然后单击下一步加载前一页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM