Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

javascript - 如何在不重新加载页面的情况下修改URL?(How do I modify the URL without reloading the page?)

Is there a way I can modify the URL of the current page without reloading the page?(有没有办法可以在不重新加载页面的情况下修改当前页面的URL?)

I would like to access the portion before the # hash if possible.(如果可能,我想访问#哈希之前的部分。) I only need to change the portion after the domain, so it's not like I'm violating cross-domain policies.(我只需要更改域的部分,所以就好像我没有违反跨域策略一样。) window.location.href = "www.mysite.com/page2.php"; // Sadly this reloads   ask by Robinicks translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This can now be done in Chrome, Safari, Firefox 4+, and Internet Explorer 10pp4+!(现在,可以在Chrome,Safari,Firefox 4+和Internet Explorer 10pp4 +中完成此操作!)

See this question's answer for more information: Updating address bar with new URL without hash or reloading the page(有关更多信息,请参见此问题的答案: 使用新URL更新地址栏而不散列或重新加载页面) Example:(例:) function processAjaxData(response, urlPath){ document.getElementById("content").innerHTML = response.html; document.title = response.pageTitle; window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath); } You can then use window.onpopstate to detect the back/forward button navigation:(然后,您可以使用window.onpopstate来检测后退/前进按钮导航:) window.onpopstate = function(e){ if(e.state){ document.getElementById("content").innerHTML = e.state.html; document.title = e.state.pageTitle; } }; For a more in-depth look at manipulating browser history, see this MDN article .(有关操纵浏览器历史记录的更深入的信息,请参阅此MDN文章 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...