javascript 更改 ajax 请求的 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6955430/
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
Change url on ajax request
提问by bliof
I want to add some GET
parameters to the page URL. I am loading content like this:
我想向GET
页面 URL添加一些参数。我正在加载这样的内容:
$("#content").load("shop.pl #content .product", $.param({categ:1)}));
How can I change the URL to have the same parameter ?categ=1
?
如何更改 URL 以具有相同的参数?categ=1
?
回答by Daniel Baulig
You can use the HTML5 pushState APIto achieve this. However, only the newest browsers support it and IE doesn't even support it yet in IE9. There was no way to do this previsouly. However, there is a convention that you would use location.hash to indicate a page which's state was modified by an ajax request. For more information on how to use url hashes to indicate state on your page you might want to have a look at this.
您可以使用 HTML5 pushState API来实现这一点。但是,只有最新的浏览器支持它,而 IE 甚至在 IE9 中都不支持它。以前没有办法做到这一点。但是,有一个约定,您将使用 location.hash 来指示状态已被 ajax 请求修改的页面。有关如何使用 url 哈希指示页面状态的更多信息,您可能需要查看此。
There is a library, History.js, which can be used for transition to pushState. It's API is very close to the pushState API, but it will transparently provide a hash fragement fallback if the browser does not support pushState.
有一个库History.js可用于转换到 pushState。它的 API 与 pushState API 非常接近,但如果浏览器不支持 pushState,它将透明地提供哈希片段回退。
回答by andyb
Your content is being loaded via AJAX, so perhaps the URL should have it's hash fragmentmodified - see http://code.google.com/web/ajaxcrawling/docs/specification.html
您的内容是通过 AJAX 加载的,因此 URL 可能应该修改其哈希片段- 请参阅http://code.google.com/web/ajaxcrawling/docs/specification.html
回答by Tadeck
You can try to manipulate the visible URL using pushState()
method (see documentation, you may wish to read also about onpopstate
event).
您可以尝试使用pushState()
method操作可见 URL (请参阅文档,您可能还希望阅读有关onpopstate
event 的内容)。
That way you can change the URL in a way you wish.
这样您就可以按照您希望的方式更改 URL。
Some demonstration on how to change URL without page reloading is here: http://html5demos.com/history
关于如何在不重新加载页面的情况下更改 URL 的一些演示如下:http: //html5demos.com/history