最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change url on ajax request - Stack Overflow

programmeradmin1浏览0评论

I want to add some GET parameters to the page URL. I am loading content like this:

$("#content").load("shop.pl #content .product", $.param({categ:1)}));

How can I change the URL to have the same parameter ?categ=1?

I want to add some GET parameters to the page URL. I am loading content like this:

$("#content").load("shop.pl #content .product", $.param({categ:1)}));

How can I change the URL to have the same parameter ?categ=1?

Share Improve this question edited Aug 5, 2011 at 11:34 andyb 43.8k12 gold badges125 silver badges153 bronze badges asked Aug 5, 2011 at 11:20 bliofbliof 2,9872 gold badges25 silver badges40 bronze badges 1
  • Not sure you can do it without changing the page. – Nalum Commented Aug 5, 2011 at 11:30
Add a comment  | 

3 Answers 3

Reset to default 12

You can use the HTML5 pushState API to 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.

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.

Your content is being loaded via AJAX, so perhaps the URL should have it's hash fragment modified - see http://code.google.com/web/ajaxcrawling/docs/specification.html

You can try to manipulate the visible URL using pushState() method (see documentation, you may wish to read also about onpopstate event).

That way you can change the URL in a way you wish.

Some demonstration on how to change URL without page reloading is here: http://html5demos.com/history

发布评论

评论列表(0)

  1. 暂无评论