aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/history/state/index.html
blob: 7b59ecb5ebc74835088638ceae5df816ee0fb25c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
title: state
slug: Web/API/History/state
translation_of: Web/API/History/state
---
<p>返回在 history 栈顶的 <code>任意</code> 值的拷贝。 通过这种方式可以查看 state 值,不必等待 <code><a href="https://developer.mozilla.org/en-US/docs/Web/Events/popstate" title="/en-US/docs/Web/Events/popstate">popstate</a></code>事件发生后再查看。</p>

<h2 id="语法">语法</h2>

<pre class="brush: js">let currentState = history.state;</pre>

<p>如果不进行下面两种类型的调用,state 的值将会是 null </p>

<p>{{domxref("History.pushState","pushState()")}} or {{domxref("History.replaceState","replaceState()")}}.</p>

<h2 id="例子">例子</h2>

<p>下面 log 例句输出 history.state 的值,首先是在没有执行 {{domxref("History.pushState","pushState()")}} 语句进而将值 push 到 history 之前的 log。下面一行 log 语句再次输出 state 值,此时 history.state 已经有值。可以在脚本文件中执行下面语句,或者在控制台直接执行。</p>

<pre class="brush: js">// 值为 null 因为我们还没有修改 history 栈
console.log(`History.state before pushState: ${history.state}`);

// 现在 push 一些数据到栈里
history.replaceState({name: 'Example'}, "pushState example", 'page3.html');

// 现在 state 已经有值了
console.log(`History.state after pushState: ${history.state}`);</pre>

<h2 id="SpecificationsE">Specifications<a class="button section-edit only-icon" href="https://developer.mozilla.org/en-US/docs/Web/API/History$edit#Specifications" rel="nofollow, noindex"><span>E</span></a></h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td><a class="external external-icon" href="https://html.spec.whatwg.org/multipage/browsers.html#the-history-interface" hreflang="en" lang="en" rel="noopener">HTML Living Standard<br>
    <small lang="en-US">The definition of 'History' in that specification.</small></a></td>
   <td><span class="spec-Living">Living Standard</span></td>
   <td>Adds the <code>scrollRestoration</code> attribute.</td>
  </tr>
  <tr>
   <td><a class="external external-icon" href="https://www.w3.org/TR/html50/browsers.html#the-history-interface" hreflang="en" lang="en" rel="noopener">HTML5<br>
    <small lang="en-US">The definition of 'History' in that specification.</small></a></td>
   <td><span class="spec-REC">Recommendation</span></td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td><a class="external external-icon" href="https://majido.github.io/scroll-restoration-proposal/history-based-api.html#web-idl" hreflang="en" lang="en" rel="noopener">Custom Scroll Restoration - History-based API<br>
    <small lang="en-US">The definition of 'History' in that specification.</small></a></td>
   <td><span class="spec-Draft">Draft</span></td>
   <td>Adds the <code>scrollRestoration</code> attribute.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("api.History.state")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/History_API/Working_with_the_History_API">Working with the History API</a></li>
</ul>