gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:33:29 27 4
gpt4 key购买 nike

我用 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-changing-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 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.

...仅举几例。

<小时/>

(!!)注意

使用 pushState 时的一个重要副作用(引自 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.

关于javascript - 如何在不刷新页面的情况下更改 URI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com