在IE9中使用history.pushstate

时间:2013-01-15 17:05:42

标签: javascript jquery html internet-explorer-9 pushstate

由于window.history.pushState不适用于像IE9这样的HTML 4浏览器,我已经找到了history.js,这是一个模拟pushState行为的jQuery库。

问题是,当使用pushState时,url的结尾是重复的

例如,

History.pushState(null,null,window.location.pathname + '?page=1');

返回,

http://www.development.com/test.html#test.html?page=1

如何避免此问题?谢谢你。

更新(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
                }

我仍然遇到问题,如果是第一页

http://localhost/development/flipV5.html?issue=20121220&page=1

当我在IE9中进入第二页时,它有url:

http://localhost/development/flipV5.html?issue=20121220&page=1#flipV5.html?issue=20121220&page=2

我想要实现的是

http://localhost/development/flipV5.html?issue=20121220&page=2

如果HTML 4浏览器不可能,请至少实现

http://localhost/development/flipV5.html/#?issue=20121220&page=2

感谢您的帮助

4 个答案:

答案 0 :(得分:3)

您的代码完全符合您的预期。

window.location.pathname + '?page=1'

打印出位置路径名(test.html),并附加?page=1

删除window.location.pathname,它应该有用。

答案 1 :(得分:2)

您需要为IE设置客户端重定向。假设用户登陆此页面:

http://localhost/development/flipV5.html?issue=20121220&page=1

他们需要被发送到

http://localhost/development/flipV5.html#?issue=20121220&page=1

原因是IE< = 9不允许您在哈希之前修改任何内容,而不会将用户发送到新页面。当您确实在facebook.com时,这是一项旧的安全功能,无法将网址更改为spambook.com。最近,人们意识到这太过分了。

重定向如下所示:

<!--[if LTE IE 9]>
<script type="text/javascript">
if (location.search) 
    location.href = location.pathname + "#" + location.search;
</script>
<![endif]-->

您现在可以推送状态,它们不会同时显示在搜索和哈希中(分别位于?#之后)

答案 2 :(得分:1)

只需从History.pushState

中删除window.location.pathname即可
History.pushState(null,null,'?page=1');

答案 3 :(得分:0)

也许试试:

History.replaceState(null,null,'?issue=' + currPageIssueNo + '&page=' + pageNo);
相关问题