diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/mozilla/firefox/releases/1.5/using_firefox_1.5_caching | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/mozilla/firefox/releases/1.5/using_firefox_1.5_caching')
-rw-r--r-- | files/zh-cn/mozilla/firefox/releases/1.5/using_firefox_1.5_caching/index.html | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/files/zh-cn/mozilla/firefox/releases/1.5/using_firefox_1.5_caching/index.html b/files/zh-cn/mozilla/firefox/releases/1.5/using_firefox_1.5_caching/index.html new file mode 100644 index 0000000000..b062c43ddd --- /dev/null +++ b/files/zh-cn/mozilla/firefox/releases/1.5/using_firefox_1.5_caching/index.html @@ -0,0 +1,184 @@ +--- +title: Using Firefox 1.5 caching +slug: Mozilla/Firefox/Releases/1.5/Using_Firefox_1.5_caching +translation_of: Mozilla/Firefox/Releases/1.5/Using_Firefox_1.5_caching +--- +<div>{{FirefoxSidebar}}</div><h2 id="Introduction" name="Introduction">介绍</h2> + +<p><a href="/en-US/docs/Firefox_1.5_for_developers" title="Firefox_1.5_for_developers">Firefox 1.5</a> 对整个web页面的一个浏览器会话进行了内存缓存,包括他们对JavaScript状态。在访问过的页面间后退和前进不需要页面加载同时保存JavaScript状态。这个特性,被称为bfcache(“后退前进缓存”),使得页面导航(切换)非常快。这个状态缓存被保存直到用户关闭浏览器。 </p> + +<p>有时候,Firefox不缓存页面。下面是一些页面不被缓存的常见的编程的原因:</p> + +<ul> + <li>页面使用 <code>unload</code> 或者 <code>beforeunload</code> 处理程序;</li> + <li>页面设置 "cache-control: no-store".</li> + <li>页面是 HTTPS 同时页面至少有一个以下设置: + <ul> + <li>"Cache-Control: no-cache"</li> + <li>"Pragma: no-cache"</li> + <li>使用 "Expires: 0" 或者 "Expires" 设置相对于 “Date” header值的过去日期值(除非指定 "Cache-Control: max-age=");</li> + </ul> + </li> + <li>当用户导航跳离页面时页面还没有完全加载或者因为其他原因有等待(pending)的网络请求 (例如 <code>XMLHttpRequest</code>));</li> + <li>页面运行IndexedDB事件;</li> + <li>顶级页面包含frames (例如 {{HTMLElement("iframe")}})因为这里列出的任何原因 而没有被缓存;</li> + <li>页面是在frame内而且用户在这个框架中加载一个新页面(在这种情况下, 当用户离开这个页面,最后加载入frames的内容会被缓存)。</li> +</ul> + +<p>这个新的缓存特性改变了页面的加载行为,web作者也许希望: </p> + +<ul> + <li>知晓被导航到的页面(当它从用户缓存中被加载)</li> + <li>在用户离开页面时定义页面行为 (同时还支持页面缓存)</li> +</ul> + +<p>两个新的浏览器事件使得用户可以做到这2个要求。</p> + +<h2 id="New_browser_events" name="New_browser_events">新的浏览器事件</h2> + +<p>如果你使用新的事件,你的页面在其他浏览器依然会正确显示 (我们已经测试了早期版本的Firefox, Internet Explorer, Opera, 和 Safari),而且在 Firefox 1.5加载时将会使用新的缓存功能。</p> + +<p>注意: 直至10-2009 开发版本的Safari 添加了这些新的事件的支持 (见 <a class="link-https" href="https://bugs.webkit.org/show_bug.cgi?id=28758" title="https://bugs.webkit.org/show_bug.cgi?id=28758">the webkit bug</a>).</p> + +<p>标准的页面行为是:</p> + +<ol> + <li>用户导航至一个页面</li> + <li>当页面加载,行内scripts执行。</li> + <li>一旦页面加载完毕, <code>onload</code> 事件执行。</li> +</ol> + +<p>有些页面包含第四步。如果一个页面使用 <code>unload</code> 或者 <code>beforeunload</code> 处理程序,当从页面导航离开时事件被执行。如果提供了一个<code>unload</code> 处理程序,页面将不会被缓存。</p> + +<p>当一个用户导航至一个被缓存的页面,行内scripts和<code>onload</code> 处理程序将不会执行 (步骤 2 和 3), 因为在大多数情况下,这些scripts的影响(执行效果)已经被保存。</p> + +<p>如果你想在每次用户导航到页面时都执行页面包含的scripts或者其他在页面加载期间执行的行为,或者你想知道什么时候用户导航至一个被缓存的页面,使用新的<code>pageshow</code> 事件.</p> + +<p>如果你拥有当用户导航离页面是执行的行为,但是你想利用这个新的缓存功能,因此不想使用unload处理程序,使用新的 <code>pagehide</code> 事件</p> + +<h3 id="pageshow_event" name="pageshow_event">pageshow 事件</h3> + +<p>这个事件和 <code>load</code> 事件一样的工作(效果), 除了它每次页面加载是都执行 that it fires every time the page is loaded (然而 <code>load</code> 事件在 Firefox 1.5中当页面从缓存中加载时不执行). 页面第一次加载时, <code>pageshow</code> 事件在 <code>load</code> 事件执行后执行。 <code>pageshow</code> 事件使用一个命名为<code>persisted</code> 的boolean属性,在初始加载时默认设置为 <code>false</code>。如果它不是初始加载,被设置为<code>true</code>(换句话说,但页面被缓存时它被设置为true).</p> + +<p>在 <code>pageshow</code> 事件执行时,设置每次页面加载时你想要运行的任何的JavaScript。</p> + +<p>如果你将JavaScript函数作为pageshow事件的一部分调用,你可以通过调用<code>pageshow</code> 事件作为<code>load</code>事件的一部分确保在不同于Firefox 1.5的浏览器在页面加载时调用这些函数,如在本文后面所示的示例。</p> + +<h3 id="pagehide_event" name="pagehide_event">pagehide 事件</h3> + +<p>如果你想定义当用户导航离页面时的行为,但是你不想使用 <code>unload</code> (这将导致页面不被缓存),你可以使用新的 <code>pagehide</code> 事件。像 <code>pageshow</code> , <code>pagehide</code> 事件使用一个命名为<code>persisted</code> 的属性。 这个属性在页面未被浏览器缓存时设置为 <code>false</code> ,如果页面被浏览器缓存则设置为 <code>true</code> 。如果这个属性被设置为 <code>false</code>, 如果有设置<code>unload</code> 处理程序的话,<code>unload</code> 事件在<code>pagehide</code> 事件执行后马上执行。</p> + +<p>当页面初次加载,Firefox 1.5 试图按事件会发生相同的顺序模拟load事件。Frames 被作为顶级文档一样对待。如果页面包含frames, 则当缓存当页面被加载:</p> + +<ul> + <li>每个frame的<code>pageshow</code> 事件在主文档的 <code>pageshow</code> 事件之前执行。</li> + <li>当用户导航离被缓存的页面, 每个frame的<code>pagehide</code> 事件在主文档的 <code>pagehide</code> 事件之前执行。</li> + <li>发生在一个 frame 的导航, 只有在受影响的frame触发事件。</li> +</ul> + +<h2 id="示例代码">示例代码</h2> + +<p>以下的示例演示了一个页面同时使用 <code>load</code> 和 <code>pageshow</code> 事件。这个示例页面行为如下:</p> + +<ul> + <li>在除了Firefox 1.5的浏览器,以下在每次页面加载都发生:<code>load</code> 事件触发<code>onLoad</code> 函数,该函数调用 <code>onPageShow</code> 函数 (如同一个附加功能).</li> + <li>在Firefox 1.5,页面第一次加载时 <code>load</code> 事件和其他浏览器上运行一样。 除了, <code>pageshow</code> 事件的执行和 <code>persisted</code> 设置为 <code>false</code>,没有额外的行动发生。</li> + <li>在 Firefox 1.5, 当页面从缓存中被加载,只有 <code>pageshow</code> 事件执行。随着 <code>persisted</code> 被设置为 <code>true</code>,只有 <code>onPageShow</code> 方法中的JavaScript行为被触发。</li> +</ul> + +<p>在这个例子中:</p> + +<ul> + <li>每次页面加载,页面计算并显示当前日期和时间。这个计算包括秒和毫秒,所以你可以很容易地测试功能。</li> + <li>第一次加载页面,光标放置在表单的Name字段。在 Firefox 1.5, 当用户导航回该页面,光标依然在页面导航离开时的位置。在其他浏览器,光标移回到Name字段。</li> +</ul> + +<pre class="brush:html"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" + "http://www.w3.org/TR/html4/loose.dtd"> +<HTML> +<head> +<title>Order query Firefox 1.5 Example</title> +<style type="text/css"> +body, p { + font-family: Verdana, sans-serif; + font-size: 12px; + } +</style> +<script type="text/javascript"> +function onLoad() { + loadOnlyFirst(); + onPageShow(); +} + +function onPageShow() { +//calculate current time + var currentTime= new Date(); + var year=currentTime.getFullYear(); + var month=currentTime.getMonth()+1; + var day=currentTime.getDate(); + var hour=currentTime.getHours(); + var min=currentTime.getMinutes(); + var sec=currentTime.getSeconds(); + var mil=currentTime.getMilliseconds(); + var displayTime = (month + "/" + day + "/" + year + " " + + hour + ":" + min + ":" + sec + ":" + mil); + document.getElementById("timefield").value=displayTime; +} + +function loadOnlyFirst() { + document.zipForm.name.focus(); +} +</script> +</head> +<body onload="onLoad();" onpageshow="if (event.persisted) onPageShow();"> +<h2>Order query</h2> + +<form name="zipForm" action="http://www.example.com/formresult.html" method="get"> +<label for="timefield">Date and time:</label> +<input type="text" id="timefield"><br> +<label for="name">Name:</label> +<input type="text" id="name"><br> +<label for="address">Email address:</label> +<input type="text" id="address"><br> +<label for="order">Order number:</label> +<input type="text" id="order"><br> +<input type="submit" name="submit" value="Submit Query"> +</form> +</body> +</html> +</pre> + +<p>与此相反, 如果以上页面不监听 <code>pageshow</code> 事件,所有的计算作为 <code>load</code> 事件的一部分(取而代之的是如下面的示例代码片段所示的编码),Firefox 1.5 中,当用户导航离页面时,光标位置及date/time会被缓存。当用户返回回该页面,缓存的date/time将会显示。</p> + +<pre class="brush:html"><script> +function onLoad() { + loadOnlyFirst(); + +//calculate current time + var currentTime= new Date(); + var year = currentTime.getFullYear(); + var month = currentTime.getMonth()+1; + var day = currentTime.getDate(); + var hour=currentTime.getHours(); + var min=currentTime.getMinutes(); + var sec=currentTime.getSeconds(); + var mil=currentTime.getMilliseconds(); + var displayTime = (month + "/" + day + "/" + year + " " + + hour + ":" + min + ":" + sec + ":" + mil); + document.getElementById("timefield").value=displayTime; +} + +function loadOnlyFirst() { + document.zipForm.name.focus(); +} +</script> +</head> + +<body onload="onLoad();"> +</pre> + +<h2 id="Developing_Firefox_extensions" name="Developing_Firefox_extensions">开发Firefox扩展</h2> + +<p>Firefox 1.5 <a href="/en-US/docs/Building_an_Extension" title="Building_an_Extension">extensions</a> 需要允许缓存功能。如果你在开发一个兼容1.5 及以前版本的Firefox扩展,确保它监听事件触发 <code>load</code> 可被缓存,监听的 <code>pageshow</code> 事件触发不应该被缓存。</p> + +<p>例如,Firefox的Google工具栏为了兼容1.5和更早的版本,应该为autolink监听 <code>load</code> 事件函数,为PageRank监听 <code>pageshow</code> 事件函数。</p> |