javascript 在 IE9 中使用 history.pushstate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14342912/
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
Using history.pushstate in IE9
提问by user782104
Since window.history.pushState is not aviliable for HTML 4 browsers like IE9 , I have looked for history.js, a jQuery library that simulates the pushState behavior.
由于 window.history.pushState 不适用于 IE9 等 HTML 4 浏览器,因此我寻找 history.js,这是一个模拟 pushState 行为的 jQuery 库。
The problem is , when using pushState, the end of the url is duplicated
问题是,当使用pushState时,url的结尾重复了
For example,
例如,
History.pushState(null,null,window.location.pathname + '?page=1');
returns,
返回,
http://www.development.com/test.html#test.html?page=1
http://www.development.com/test.html#test.html?page=1
How do I avoid this problem? Thank you kindly.
我该如何避免这个问题?非常感谢你。
Update (On 2012 / 1 /22) , Question for bounty:
更新(2012 年 1 月 22 日),赏金问题:
if (pageNo == 1){
//window.history.pushState({"html":currURL,"pageTitle":''},"", window.location.pathname + '?page=1'); For chrome and FX only
//History.replaceState(null,null,'')
//History.pushState(null,null,'?issue=' + currPageIssueNo + '&page=1');
}
else if (pageNo != 1 || pageNo == 1 && linkPageNo > 1 ) {
History.pushState(null,null,'?issue=' + currPageIssueNo + '&page=' + pageNo);
//window.history.pushState({"html":currURL,"pageTitle":''},"", newURL); For chrome and FX only
}
I am still encounter the problem , if it is the first page
我还是遇到这个问题,如果是第一页
http://localhost/development/flipV5.html?issue=20121220&page=1
When i go to second page in IE9 , it has url :
当我在 IE9 中转到第二页时,它有 url :
http://localhost/development/flipV5.html?issue=20121220&page=1#flipV5.html?issue=20121220&page=2
Which i would like to achieve is
我想实现的是
http://localhost/development/flipV5.html?issue=20121220&page=2
If it is impossible for HTML 4 browser, please at least achieve
如果 HTML 4 浏览器无法实现,请至少实现
http://localhost/development/flipV5.html/#?issue=20121220&page=2
thanks for kindly help
感谢您的帮助
回答by Christian
Your code does exactly what you'd expect it to.
您的代码完全符合您的预期。
window.location.pathname + '?page=1'
Prints out the location pathname (test.html
), and appends ?page=1
打印出位置路径名 ( test.html
),并附加?page=1
Remove window.location.pathname
and it should work.
删除window.location.pathname
它应该可以工作。
回答by Brigand
You need to set up a client-side redirect for IE. Assuming the user lands at this page:
您需要为 IE 设置客户端重定向。假设用户登陆此页面:
http://localhost/development/flipV5.html?issue=20121220&page=1
They need to be sent to
他们需要被送到
http://localhost/development/flipV5.html#?issue=20121220&page=1
The reason is that IE <= 9 doesn't let you modify anything before the hash, without sending the user to a new page. It's an old security feature preventing changing the URL to facebook.com
, when you're really on spambook.com
. Recently, people have realized that that's overkill.
原因是 IE <= 9 不允许您在哈希之前修改任何内容,而不会将用户发送到新页面。这是一项旧的安全功能,可防止facebook.com
在您真正使用 时将 URL 更改为spambook.com
。最近,人们意识到这太过分了。
The redirect looks like this:
重定向如下所示:
<!--[if LTE IE 9]>
<script type="text/javascript">
if (location.search)
location.href = location.pathname + "#" + location.search;
</script>
<![endif]-->
You can now push states, and they won't show up in both the search and the hash (that's after the ?
and #
respectively)
您现在可以推送状态,并且它们不会同时出现在搜索和哈希中(分别在?
和之后#
)
回答by Musa
Just remove window.location.pathname from History.pushState
只需从中删除 window.location.pathname History.pushState
History.pushState(null,null,'?page=1');
回答by lukaszkups
Maybe try:
也许尝试:
History.replaceState(null,null,'?issue=' + currPageIssueNo + '&page=' + pageNo);