Javascript 在不重新加载页面的情况下更改 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3381280/
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
Changing the URL without reloading the page
提问by Paul Peelen
I would like to know if it's possible to change the contents of the URL in the browser without reloading the page?
我想知道是否可以在不重新加载页面的情况下更改浏览器中 URL 的内容?
I use jQuery and Ajax to load new parts of my page. When I choose "product one", the direct link would be mysite.com/product1and for "product two" would be mysite.com/product2, but I don't want to reload the site to these pages.
我使用 jQuery 和 Ajax 加载页面的新部分。当我选择“产品一”时,直接链接将是mysite.com/product1“产品二” mysite.com/product2,但我不想将站点重新加载到这些页面。
采纳答案by Sarfraz
You will have to add hash #if you want to prevent page from reloading.
#如果要防止页面重新加载,则必须添加哈希。
The css-tricks.com has an excellent screencast on that, have a look at:
css-tricks.com 有一个很好的截屏视频,看看:
回答by Pavel
its now possible with HTML_5..
现在可以使用 HTML_5..
chack this link... http://www.spoiledmilk.dk/blog/?p=1922
破解此链接... http://www.spoiledmilk.dk/blog/?p=1922
also facebook and google using this tric beside Hash(#) attribute
在 Hash(#) 属性旁边也使用这个 tric 的 facebook 和 google
回答by Sparky
This is possible in HTML5. See a demo here.
这在 HTML5 中是可能的。在此处查看演示。
You can change the URL to another URL within the same domain, but can not change the domain for security reasons.
您可以将 URL 更改为同一域内的另一个 URL,但出于安全原因不能更改域。
See the historyinterface in HTML5 specificationfor details.
有关详细信息,请参阅HTML5 规范中的history接口。
回答by naveen
just use this one
就用这个
window.history.pushState("object or string", "Title", "/new-url");
回答by Attila O.
You CANdo that. Though likely you'll need a modern browser. Have a look at this page: http://www.20thingsilearned.com/created by the Google Chrome team (I used Chrome 9 to read it). Changing pages doesn't reload the entire web page, but changes the URL.
你CAN做到这一点。尽管您可能需要一个现代浏览器。看看这个页面:http: //www.20thingsilearned.com/由 Google Chrome 团队创建(我使用 Chrome 9 阅读它)。更改页面不会重新加载整个网页,但会更改 URL。
回答by Manolis Georgopoulos
Yes, it is possible using the HTML5 History API. Check this pageand this example
回答by Ionu? Staicu
You can't. Only if you change the hash, like sAc told you.
你不能。仅当您更改哈希值时,就像 sAc 告诉您的那样。
But.. May I ask WHY?
但是..我可以问为什么吗?

