如何在不刷新页面的情况下更改 URI?
我用 CodeIginer 构建了一个网站,我想在我的一个页面中实现 AJAX 和 JQuery。问题是当我加载内容时,URL 没有改变。
假设我有以下 URI:
http://www.example.com/controller/function/param
http://www.example.com/controller/function/param2
code>
单击按钮时如何从第一个更改为第二个?
I have built a website with CodeIginer and I want to implement AJAX and JQuery in one of my pages. The problem is that when I load the content, the URL does not change.
Let’s say that I have these URI:
http://www.example.com/controller/function/param
http://www.example.com/controller/function/param2
How can I change from the first one to the second one when I click a button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 HTML5 中,您可以更改 URL:
检查 http://spoiledmilk.com/ blog/html5-change-the-browser-url-without-refreshing-page/
文档:https://developer.mozilla.org/en/DOM/Manipulated_the_browser_history#The_pushState().c2。 a0method
更新
浏览器支持新的 HTML5 历史记录 API 的概述:
http://caniuse.com/#search=pushState(caniuse.com 值得收藏!)
已经有框架可以执行以下操作为您努力工作,甚至优雅地回退到常见的哈希标签解决方案:
History.js
Backbone.js
Mootools(通过插件)
dojo 工具包
...仅举几例。
(!!)注意
使用
pushState
时的一个重要副作用(引自骨干文档):In HTML5 you can change the URL:
check http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/
docs: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState().c2.a0method
UPDATE
An overview of which browser support the new HTML5 history API:
http://caniuse.com/#search=pushState (caniuse.com is worth to bookmark!)
there are already frameworks that do the hard work for you and even gracefully fallback to the common hash-tag solution:
History.js
Backbone.js
Mootools (via Plugin)
dojo toolkit
... just to name a few.
(!!) BE AWARE
One important side-effect when using the
pushState
(citation from the Backbone documentation):可以使用哈希 (#) 并在后面添加您喜欢的任何内容。
这是我使用它构建的一个网站 - 然后我让 JavaScript 读取哈希值并调用适当的函数:
http://bannerhouse.com.au/#/popup=media&id=don
旁注:
这对于 Flash 网站或 Flash 内容也很有用;您可以使用 FlashVars 将哈希值解析为 SWF,并根据该值加载适当的部分/屏幕。
Could use a hash (#) and put whatever you like afterwards.
Here's a site I built using this - and then I have JavaScript read the hash and call appropriate functions:
http://bannerhouse.com.au/#/popup=media&id=don
Side note:
This is useful for flash websites or flash content as well; you can use FlashVars to parse the hash value to the SWF and load an appropriate section/screen based on that.
通过 Javascript 使用哈希标签,因此在按钮的单击事件处理程序中:
location.hash = "param2"
这将改变 http://example.com/mypage/#whatever
到
http://example.com/mypage/#param2
当然,你也可以把你的“文件夹" 在哈希之后,因此,使用 http://example.com/ 的基本 url,然后添加:
location.hash = "/MyPage/MySubPage/MyInfo";
将其更改为 http://example.com/#/MyPage/MySubPage/MyInfo
Use hash tags via Javascript, so in the button's click event handler:
location.hash = "param2"
Which will change http://example.com/mypage/#whatever
to
http://example.com/mypage/#param2
Of course, you could also put your "folders" after the hash, so, with a base url of http://example.com/ you then add:
location.hash = "/MyPage/MySubPage/MyInfo";
which changes it to http://example.com/#/MyPage/MySubPage/MyInfo
类似的线程推断出否,除非使用哈希标签。
提出但强烈反对的一个想法是使用 204 HTTP 响应。
来自W3:
A similar thread deduced no, unless using a hash tag.
One idea put forward, but strongly discouraged, was using a 204 HTTP response.
From the W3:
以下是 WHATWG 的 HTML Living Standard(以前称为 HTML5)的相关摘录,§7.10.2 浏览上下文的会话历史记录:
Here is the relevant excerpt from WHATWG’s HTML Living Standard (formerly known as HTML5), § 7.10.2 The session history of browsing contexts: