In an
HTML
document, the
history.pushState()
method adds a state to the browser's session history stack.
history.pushState(state, title[, url])
state
pushState()
. Whenever the user navigates to the new state, a
popstate
event is fired, and the
state
property of the event contains a copy of the history entry's state object.
pushState()
, the method will throw an exception. If you need more space than this, you're encouraged to use
sessionStorage
and/or
localStorage
.
title
url
可选
pushState()
, but it might attempt to load the URL later, for instance after the user restarts the browser. The new URL does not need to be absolute; if it's relative, it's resolved relative to the current URL. The new URL must be of the same
origin
as the current URL; otherwise,
pushState()
will throw an exception. If this parameter isn't specified, it's set to the document's current URL.
In a sense, calling
pushState()
is similar to setting
window.location = "#foo"
, in that both will also create and activate another history entry associated with the current document. But
pushState()
has a few advantages:
window.location
keeps you at the same document only if you modify only the hash.
window.location = "#foo";
only creates a new history entry if the current hash isn't
#foo
.
注意,
pushState()
never causes a
hashchange
event to be fired, even if the new URL differs from the old URL only in its hash.
This creates a new browser history state setting the state , title ,和 url .
const state = { 'page_id': 1, 'user_id': 5 }
const title = ''
const url = 'hello-world.html'
history.pushState(state, title, url)
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'History.pushState()' in that specification. |
实时标准 | 无变化自 HTML5 . |
|
HTML5
The definition of 'History.pushState()' in that specification. |
推荐 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
pushState
|
Chrome 5 | Edge 12 |
Firefox
4
|
IE 10 | Opera 11.5 | Safari 5 | WebView Android ≤37 | Chrome Android 18 |
Firefox Android
4
|
Opera Android 11.5 | Safari iOS 4.3 | Samsung Internet Android 1.0 |
是否
title
argument is used
|
Chrome No | Edge No | Firefox No | IE No | Opera No | Safari Yes | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS ? | Samsung Internet Android No |
完整支持
不支持
兼容性未知
见实现注意事项。