[英]Using history.pushstate in IE9
由於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
感謝您的幫助
您的代碼完全符合您的期望。
window.location.pathname + '?page=1'
打印出位置路徑名( test.html
),並附加?page=1
刪除window.location.pathname
,它應該可以工作。
您需要為IE設置客戶端重定向。 假設用戶登陸此頁面:
http://localhost/development/flipV5.html?issue=20121220&page=1
他們需要發送到
http://localhost/development/flipV5.html#?issue=20121220&page=1
原因是IE <= 9不允許您在哈希之前修改任何內容,而不會將用戶發送到新頁面。 當您確實在spambook.com
上時,它是一項舊的安全功能,可防止將URL更改為facebook.com
。 最近,人們意識到這太過分了。
重定向如下所示:
<!--[if LTE IE 9]>
<script type="text/javascript">
if (location.search)
location.href = location.pathname + "#" + location.search;
</script>
<![endif]-->
現在,您可以推送狀態,它們不會同時出現在搜索和哈希中(分別在?
和#
之后)
只需從History.pushState
刪除window.location.pathname
History.pushState(null,null,'?page=1');
也許嘗試:
History.replaceState(null,null,'?issue=' + currPageIssueNo + '&page=' + pageNo);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.