History.pushState() - Web APIs 编辑
In an HTML document, the history.pushState()
method adds an entry to the browser's session history stack.
Syntax
history.pushState(state, title [, url])
Parameters
state
- The
state
object is a JavaScript object which is associated with the new history entry created bypushState()
. Whenever the user navigates to the newstate
, apopstate
event is fired, and thestate
property of the event contains a copy of the history entry'sstate
object. - The
state
object can be anything that can be serialized. Because Firefox savesstate
objects to the user's disk so they can be restored after the user restarts the browser, we impose a size limit of 640k characters on the serialized representation of astate
object. If you pass astate
object whose serialized representation is larger than this topushState()
, the method will throw an exception. If you need more space than this, you're encouraged to usesessionStorage
and/orlocalStorage
. title
- Most browsers currently ignore this parameter, although they may use it in the future. Passing the empty string here should be safe against future changes to the method. Alternatively, you could pass a short title for the state to which you're moving. If you need the title to be changed you could use
document.title
. url
Optional- The new history entry's URL is given by this parameter. Note that the browser won't attempt to load this URL after a call to
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.
Description
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:
- The new URL can be any URL in the same origin as the current URL. In contrast, setting
window.location
keeps you at the same document only if you modify only the hash. - You don't have to change the URL if you don't want to. In contrast, setting
window.location = "#foo";
only creates a new history entry if the current hash isn't#foo
. - You can associate arbitrary data with your new history entry. With the hash-based approach, you need to encode all of the relevant data into a short string.
Note that pushState()
never causes a hashchange
event to be fired, even if the new URL differs from the old URL only in its hash.
Examples
This creates a new browser history entry setting the state, title, and url.
JavaScript
const state = { 'page_id': 1, 'user_id': 5 }
const title = ''
const url = 'hello-world.html'
history.pushState(state, title, url)
Change a query parameter
const url = new URL(window.location);
url.searchParams.set('foo', 'bar');
window.history.pushState({}, '', url);
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'History.pushState()' in that specification. | Living Standard | No change from HTML5. |
HTML5 The definition of 'History.pushState()' in that specification. | Recommendation | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论