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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6118693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:19:24  来源:igfitidea点击:

How can I change the page URL without refreshing the page?

javascriptjqueryajax

提问by Bobby Stenly

How do I change the URL without refreshing the page?

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

I've built a website with CodeIginer and I want to implement AJAX and JQuery in one of my pages. The problem is; when I load the content, the URL doesn't change.

我已经用 CodeIginer 建立了一个网站,我想在我的一个页面中实现 AJAX 和 JQuery。问题是; 当我加载内容时,URL 不会改变。

Let's say that I have the URL http://www.example.com/controller/function/param
And another URL : http://www.example.com/controller/function/param2

假设我有 URLhttp://www.example.com/controller/function/param
和另一个 URL:http://www.example.com/controller/function/param2

How can I change the first URL to the second one when I click a button?

单击按钮时,如何将第一个 URL 更改为第二个 URL?

回答by roberkules

In HTML5 you can change the URL:

在 HTML5 中,您可以更改 URL:

window.history.pushState("object or string", "Title", "/new-url");

check http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/

检查http://spoiledmilk.com/blog/html5-changed-the-browser-url-without-refreshing-page/

docs: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState().c2.a0method

文档: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:

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

http://caniuse.com/#search=pushState(caniuse.com is worth to bookmark!)

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

there are already frameworks that do the hard work for you and even gracefully fallback to the common hash-tag solution:

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

History.js

历史.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.

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

Backbone.js

主干.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.

Backbone 通过提供具有键值绑定和自定义事件的模型、具有可枚举函数的丰富 API 的集合、具有声明性事件处理的视图,并通过 RESTful JSON 接口将所有这些连接到您现有的应用程序,从而为 JavaScript 密集型应用程序提供结构。... pushState 支持在 Backbone 中完全基于选择加入。不支持 pushState 的旧浏览器将继续使用基于哈希的 URL 片段,如果一个具有 pushState 功能的浏览器访问了一个哈希 URL,它将透明地升级为真实的 URL。

Mootools(via Plugin)

Mootools(通过插件)

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.

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

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.

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

... just to name a few.

... 仅举几个。



(!!) BE AWARE

(!!) 意识到

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

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

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.

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

回答by Marty

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:

这是我使用它构建的一个站点 - 然后我让 JavaScript 读取哈希并调用适当的函数:

http://bannerhouse.com.au/#/popup=media&id=don

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.

这对于 Flash 网站或 Flash 内容也很有用;您可以使用 FlashVars 将哈希值解析为 SWF,并基于此加载适当的部分/屏幕。

回答by Thomas Shields

Use hash tags via Javascript, so in the button's click event handler: location.hash = "param2"Which will change http://example.com/mypage/#whateverto http://example.com/mypage/#param2

通过 Javascript 使用哈希标签,因此在按钮的点击事件处理程序中: location.hash = "param2"这会将http://example.com/mypage/#whatever更改 为 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

当然,您也可以将“文件夹”放在哈希之后,因此,使用http://example.com/的基本网址,然后添加:将 location.hash = "/MyPage/MySubPage/MyInfo";其更改为http://example.com/#/MyPage/我的子页面/我的信息

回答by Sean Powell

A similar threaddeduced no, unless using a hash tag.

一个类似的线程推断没有,除非使用哈希标签。

One idea put forward, but strongly discouraged, was using a 204 HTTP response.

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

From the W3:

来自 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.

无响应 204

服务器收到请求但没有信息发回,客户端应该保持在同一个文档视图中。这主要是为了允许在不更改文档的同时输入脚本。