如何在不刷新页面的情况下更改 URI?

发布于 2024-11-09 16:36:25 字数 286 浏览 0 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

静待花开 2024-11-16 16:36:25

在 HTML5 中,您可以更改 URL:

window.history.pushState("object or string", "Title", "/new-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

History.js 优雅地支持 HTML5 历史/状态 API
(pushState、replaceState、onPopState)在所有浏览器中。包括
继续支持数据、标题、replaceState。支持jQuery,
MooTools 和原型。对于 HTML5 浏览器,这意味着您可以
直接修改URL,不再需要使用哈希值。为了
HTML4 浏览器将恢复使用旧的 onhashchange
功能。

Backbone.js

Backbone 通过以下方式为 JavaScript 密集型应用程序提供结构:
为模型提供键值绑定和自定义事件、集合
具有丰富的可枚举函数 API、带有声明性事件的视图
处理,并将其全部连接到您现有的应用程序
RESTful JSON 接口。
...
PushState 支持纯粹是在 Backbone 中选择加入的基础上存在的。
不支持pushState的旧版浏览器将继续使用
基于哈希的 URL 片段,如果哈希 URL 被访问
支持pushState的浏览器,会透明升级为真实URL。

Mootools(通过插件)

MooTools 是一个紧凑、模块化、面向对象的 JavaScript 框架,专为中级到高级 JavaScript 开发人员而设计。 [...] 通过 popstate 或 hashchange 进行历史管理。替换页面的 URL,无需重新加载,并回退到较旧的 Hashchange
浏览器。

dojo 工具包

Dojo 使用 Web 标准作为您的开发流程,从而节省您的时间并进行扩展
它的平台。这是经验丰富的开发人员用来构建高层的工具包
高质量的桌面和移动网络应用程序。 [...] dojox.app 管理导航
通过 HTML5 PushState 标准历史记录并将其委托给启用的浏览器
历史管理。

...仅举几例。


(!!)注意

使用pushState时的一个重要副作用(引自骨干文档):

请注意,使用真实 URL 需要您的网络服务器能够
正确呈现这些页面
,因此需要对后端进行更改,如下所示
出色地。例如,如果您的路由为 /documents/100,则您的网站
如果浏览器访问该 URL,服务器必须能够提供该页面
直接地。为了获得完整的搜索引擎爬行能力,最好有
服务器为页面生成完整的 HTML ...但如果它是一个 Web
应用程序,只需呈现与您将拥有的相同内容
根 URL,并使用 Backbone Views 和 JavaScript 填充其余部分
工作正常。

In HTML5 you can change the URL:

window.history.pushState("object or string", "Title", "/new-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

History.js gracefully supports the HTML5 History/State APIs
(pushState, replaceState, onPopState) in all browsers. Including
continued support for data, titles, replaceState. Supports jQuery,
MooTools and Prototype. For HTML5 browsers this means that you can
modify the URL directly, without needing to use hashes anymore. For
HTML4 browsers it will revert back to using the old onhashchange
functionality.

Backbone.js

Backbone supplies structure to JavaScript-heavy applications by
providing models with key-value binding and custom events, collections
with a rich API of enumerable functions, views with declarative event
handling, and connects it all to your existing application over a
RESTful JSON interface.
...
pushState support exists on a purely opt-in basis in Backbone.
Older browsers that don't support pushState will continue to use
hash-based URL fragments, and if a hash URL is visited by a
pushState-capable browser, it will be transparently upgraded to the true URL.

Mootools (via Plugin)

MooTools is a compact, modular, Object-Oriented JavaScript framework designed for the intermediate to advanced JavaScript developer. [...] History Management via popstate or hashchange. Replaces the URL of the page without a reload and falls back to Hashchange on older
browsers.

dojo toolkit

Dojo saves you time and scales with your development process, using web standards as
its platform. It’s the toolkit experienced developers turn to for building high
quality desktop and mobile web applications. [...] dojox.app manage the navigation
history through HTML5 pushState standard and delegate it to browser enabled
history management.

... just to name a few.


(!!) BE AWARE

One important side-effect when using the pushState (citation from the Backbone documentation):

Note that using real URLs requires your web server to be able to
correctly render those pages
, so back-end changes are required as
well. For example, if you have a route of /documents/100, your web
server must be able to serve that page, if the browser visits that URL
directly. For full search-engine crawlability, it's best to have the
server generate the complete HTML for the page ... but if it's a web
application, just rendering the same content you would have for the
root URL, and filling in the rest with Backbone Views and JavaScript
works fine.

苦妄 2024-11-16 16:36:25

可以使用哈希 (#) 并在后面添加您喜欢的任何内容。

这是我使用它构建的一个网站 - 然后我让 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.

深居我梦 2024-11-16 16:36:25

通过 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

梦情居士 2024-11-16 16:36:25

类似的线程推断出否,除非使用哈希标签。

提出但强烈反对的一个想法是使用 204 HTTP 响应。

来自W3:

无响应204

服务器已收到请求,但
没有信息可发回,
并且客户应该保持不变
文档视图。这主要是为了让
脚本的输入而不改变
同时生成文档。

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:

No Response 204

Server has received the request but
there is no information to send back,
and the client should stay in the same
document view. This is mainly to allow
input for scripts without changing the
document at the same time.

柒夜笙歌凉 2024-11-16 16:36:25

以下是 WHATWG 的 HTML Living Standard(以前称为 HTML5)的相关摘录,§7.10.2 浏览上下文的会话历史记录

会话历史记录中的多个连续条目可以共享同一文档。当通过正常导航到达初始条目并且通过 history.pushState() 添加以下条目时,可能会发生这种情况。或者它可以通过导航到片段来发生。

Here is the relevant excerpt from WHATWG’s HTML Living Standard (formerly known as HTML5), § 7.10.2 The session history of browsing contexts:

Several contiguous entries in a session history can share the same document. This can occur when the initial entry is reached via normal navigation, and the following entry is added via history.pushState(). Or it can occur via navigation to a fragment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文