在AJAX调用更新网址是什么? [英] Update URL on AJAX call?

查看:133
本文介绍了在AJAX调用更新网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在使用AJAX的事实是,如果我使用AJAX页面上,转到另一个页面,然后使用浏览器的后退按钮返回这是与AJAX已经一去不复返了改变任何东西的最大问题。

我想过使用的jQuery编辑部地址插件来解决这个问题,但我不喜欢它只修改了URL以#whatever.html,而不是彻底改变了。

在理想情况下,我想是有距离的网址去:www.mysite.com/p:2/到www.mysite.com/p:3/当我提出的有关AJAX调用。

这是在所有可能的?

解决方案

不,这是不可能的。 更新:现在可以通过HTML5历史API ​​ - 见 razbakov的回答

我希望你意识到你正在试图解决一个非常困难的问题。

比方说,您的URL看起来像

  http://mysite.com/mypage/
 

如果您以编程方式改变窗口位置

 的http:// mysite的/我的页面/ 1 /
 

浏览器将接管并尝试导航到该页面时,就像你看中的阿贾克斯code!

那么,有什么办法呢?您可以使用URL片段。

假设你有一个这样的URL,

  http://mysite.com/anotherpage/#section
 

浏览器会先加载 http://mysite.com/anotherpage/ 并试图找到一个名为一节和滚动锚到该位置。此行为是由地址插件开发。这是类似的滚动到顶部环节是如何工作的。

所以,如果你在页面上

  http://mysite.com/mypage/
 

和更改URL,以

  http://mysite.com/mypage/#1
 

浏览器将不会加载新的页面,而是试图找到锚命名为1,并滚动到该锚点。

即使你设法片段添加到URL,这并不意味着工作已经完成。如果用户presses返回按钮,DOM将被复位,则必须解析这些碎片并重新创建DOM。这绝对是不平凡的。

Right now the biggest issue I'm having with using AJAX is the fact that if I use AJAX on a page, go to another page, then use the browser's back button to go back anything that was changed with AJAX is gone.

I've thought about using the jQuery Addresss plugin to solve that problem but I don't like how it only amends the URL with "#whatever.html" instead of changing it completely.

Ideally, what I would like is to have the URL go from: "www.mysite.com/p:2/" to "www.mysite.com/p:3/" when I make the relevant AJAX call.

Is this at all possible?

解决方案

Nope, this is not possible. Update: It is now possible via the HTML5 History API - see razbakov's answer.

I hope you realize that you are trying to address an extremely difficult problem.

Let's say your url looks like

http://mysite.com/mypage/

If you change the window location programmatically to

http://mysite/mypage/1/

Browser will take over and try to navigate to that page, there goes your fancy ajax code!

So what's the alternative? You use URL fragment.

Let's say you have a URL like this,

http://mysite.com/anotherpage/#section

Browser will first load http://mysite.com/anotherpage/ and try to find an anchor named 'section' and scroll to that location. This behavior is exploited by the 'Addresses' plugin. This is similar to how those 'Scroll To Top' links work.

So if you are on the page

http://mysite.com/mypage/

and change the URL to

http://mysite.com/mypage/#1

Browser will not load new page but rather try to find anchor named '1' and scroll to that anchor.

Even if you have managed to add fragments to the URL, it doesn't mean the work is done. If the user presses the back button, DOM will be reset and you will have to parse those fragments and recreate the DOM. It's definitely non-trivial.

这篇关于在AJAX调用更新网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆