javascript window.history.replaceState() 没有像我预期的那样工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4880424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.history.replaceState() not working as I expect
提问by simekadam
is there some way to replace url with JavaScript without reload? For example
有什么方法可以在不重新加载的情况下用 JavaScript 替换 url 吗?例如
www.url.com/index.php/presenter/view/../
to
到
www.url.com/presenter/view/../
? I've tried it with history.replaceState function but it is only changing the url after index.php..
? 我已经用 history.replaceState 函数尝试过它,但它只是在index.php..
function parseUrl(){
currUrl = window.location.href;
verify = currUrl.indexOf("index.php");
if(verify != -1){
newUrl = currUrl.substring(0,verify-1);
newUrl += '#'+currUrl.substring(currUrl.indexOf('index.php')+10,currUrl.length);
if(window.history.replaceState){
window.history.replaceState(null, this.title, newUrl);
}
else{
window.location.href = newUrl;
}
}
}
回答by balupton
Try using History.js- perhaps it's cross-browser support for the HTML5 History APIs will solve your issue.
尝试使用History.js- 也许它对 HTML5 History API 的跨浏览器支持可以解决您的问题。
回答by russau
The HTML5 History APIcan do this, but isn't supported by most browsers (only FF4, and Chrome AFAIK)
在HTML5 API历史可以做到这一点,但不被大多数浏览器(仅FF4,和Chrome AFAIK)支持
GitHub are using it for their Tree Slider.
GitHub 将它用于他们的Tree Slider。
回答by Allan Taylor
change the file system location of your application so that it reflects the url you want: www.url.com/presenter/view/
use an index.html shell
configure your web server to open index.html by default
use ajax to grab your data and insert it into your html file.
更改您的应用程序的文件系统位置,使其反映您想要的网址:www.url.com/presenter/view/
使用 index.html shell
将您的 Web 服务器配置为默认打开 index.html
使用 ajax 抓取您的数据并将其插入到您的 html 文件中。

