有沒有一種方法可以修改當前頁面的URL而無需重新加載頁面?
如果可能,我想訪問#哈希之前的部分,
我只需要更改域后的部分,所以就好像我沒有違反跨域策略一樣,
window.location.href = https://www.cnblogs.com/pythonzhilian/p/"www.mysite.com/page2.php"; // Sadly this reloads
現在,可以在Chrome,Safari,Firefox 4+和Internet Explorer 10pp4 +中完成此操作!
有關更多資訊,請參見此問題的答案: 使用新URL更新地址欄而無需哈希或重新加載頁面
例子:
function processAjaxData(response, urlPath){
document.getElementById("content").innerHTML = response.html;
document.title = response.pageTitle;
window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
}
然后,您可以window.onpopstate用來檢測后退/前進按鈕的導航:
window.onpopstate = function(e){
if(e.state){
document.getElementById("content").innerHTML = e.state.html;
document.title = e.state.pageTitle;
}
};
本文首發于前端黑洞網,博客園同步跟新
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/280520.html
標籤:Html/Css
下一篇:演算法學習:二分法從入門到精通
