blob: 118bfe284c6d1ce1e68d48ea825114adc96fed4a (
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
|
---
title: Window.history
slug: Web/API/Window/history
translation_of: Web/API/Window/history
---
<p>{{ ApiRef("Window") }}</p>
<p><strong><code>Window.history</code></strong>是一个只读属性,用来获取{{domxref("History")}} 对象的引用,{{domxref("History")}} 对象提供了操作浏览器<em>会话历史</em>(浏览器地址栏中访问的页面,以及当前页面中通过框架加载的页面)的接口。</p>
<p><code>History</code>对象有如下方法:参见<span style="line-height: 1.5;"> </span><a href="/zh-CN/docs/Web/API/History_API" style="line-height: 1.5;" title="en/DOM/Manipulating the browser history">Manipulating the browser history</a><span style="line-height: 1.5;"> 中的示例和详情。尤其指出的是文章里解释了在使用<code>pushState()</code>和<code>replaceState()方法前,你需要了解的安全问题。</code></span></p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><em>var historyObj</em> = <em>window</em>.history;</pre>
<h2 id="Example" name="Example">示例</h2>
<pre class="brush: js">history.back(); // 等同于点击浏览器的回退按钮
history.go(-1); //等同于history.back();
</pre>
<h2 id="Notes" name="Notes">附注</h2>
<p>在顶层页面中,浏览器的回退和前进按钮旁的下拉菜单显示了可以通过<code>History</code>对象访问到的页面会话历史(session history)列表。</p>
<p>出于安全考虑,History对象不允许未授权代码访问会话历史(session History)中其它页面的URLs,但可以导航到其它会话历史(session History)指向的页面。</p>
<p>未授权代码无法清除会话历史(session History),也不能禁用回退/前进功能。最快捷的可用方式是使用<a href="/en/DOM/window.location#replace" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/DOM/window.location#replace">location.replace()</a>方法,提供指定的URL来替换当前的会话历史(session history)。</p>
<h2 id="Specification" name="Specification">规范</h2>
<ul>
<li><a class="external" href="http://whatwg.org/html#the-history-interface">HTML5 History interface</a></li>
</ul>
|