如何在不刷新页面的情况下更改 URI? [英] How can I change the URI without refreshing the page?

查看:40
本文介绍了如何在不刷新页面的情况下更改 URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 CodeIginer 建立了一个网站,我想在我的一个页面中实现 AJAX 和 JQuery.问题是当我加载内容时,URL 没有改变.

假设我有这些 URI:

  • http://www.example.com/controller/function/param
  • http://www.example.com/controller/function/param2

如何在单击按钮时从第一个更改为第二个?

解决方案

在 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/Manipulating_the_browser_history#The_pushState().c2.a0method

<小时>

更新

概述哪些浏览器支持新的 HTML5 历史 API:

http://caniuse.com/#search=pushState(caniuse.com 值得收藏!)

已经有一些框架可以为您完成繁重的工作,甚至可以优雅地退回到常见的哈希标签解决方案:

History.js

<块引用>

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

Backbone.js

<块引用>

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

Mootools(通过插件)

<块引用>

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

dojo 工具包

<块引用>

Dojo 使用 Web 标准作为开发过程,为您节省时间和扩展它的平台.这是有经验的开发人员用来构建高高质量的桌面和移动 Web 应用程序.[...] dojox.app 管理导航历史通过 HTML5 pushState 标准并将其委托给启用的浏览器历史管理.

... 仅举几例.

<小时>

(!!) 注意

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

<块引用>

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

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?

解决方案

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.

这篇关于如何在不刷新页面的情况下更改 URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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