最近在做一个移动端项目,发现移动端某些返回和PC端是有差异的, 比如ios中返回按钮是直接使用缓存的, 不会执行任何js代码, 这个问题很蛋疼, 例如, 在提交的时候将按钮设置为loading状态, 如果在提交成功后没有对按钮进行处理, 那么返回后按钮依然是loading状态, 这种体验很差, 如下图:

此问题是由于某些浏览器在back的时候是直接使用的之前的视图,页面没有进行重新加载而导致的,在网上找了些资料, 发现这是H5的一些新特性Back-Forward Cache(简称bfcache) ,普通浏览器在back时,如果不是指定Cache-Control、Expires等方法强制停用Cache时,那么一般情况下浏览器大多数都会直接读取本地的缓存, 减少请求和网络传输的成本, 增加浏览的顺从度, 但Cache仅限于静态文件, 浏览器还是得重新加载html, 重新执行脚本,渲染DOM, 而bfcache则不同, 是直接读取缓存里面的html,节省了重新请求页面的时间, 既然是读取缓存的html页面, 那么执行页面的onload事件进行初始化, 会影响原本因用户操作而改变的状态, 所以浏览器在back时是不会触发onload事件.

这个时候就会产生上面的问题, 有些业务在返回时是需要重新加载的, 于是H5新增了两个事件onpageshow和onpagehide, 分别是进入网页和离开的时候触发, 即使是用浏览器的前进/后退也会触发这两个事件.

 <!DOCTYPE html>

 <html>
<head>
<title>Page Events</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.js"></script>
<script>
function dispLog(msg) {
var d = new Date();
$("<li />").text(d.toISOString().substr(14, 9) + " " + msg)
.appendTo("#dvDisp"); }
$(window).load(function () {
dispLog("Load Event");
}).ready(function () {
dispLog("Ready Event");
$("#btnSetColor").click(function () {
$("a").css("color", "red");
});
}).bind("pageshow", function () {
dispLog("PageShow Event");
}).bind("pagehide", function () {
dispLog("PageHide Event");
});
</script>
</head>
<body>
<a href="test1.html">前往其它页面</a>
<input type="button" id="btnSetColor" value="变色" />
<ul id="dvDisp"></ul>
</body>
</html>

页面很简单, 绑定onload, ready,onpageshow,onpagehide四个事件, 触发事件相应的文本会显示在页面上, 另外这里有个连接可跳转到其它网页,便于测试back, button事件会改变连接的颜色, 便于back时检查颜色是否保留,判断是否有bfcache.

测试步骤打开test.html, 点击变色按钮, 再点击"前往其它页面", 然后在test1.html点击back按钮回到test.html. 在几次测试后, 大致的测试结果如下:

IE9
打开页面或则back时都会触发Ready/Load事件, 红色未保留, 无bfcache.
IE10 (Windows 8 Release Preview)
打开页面或则back时都会触发Ready/Load事件, 红色未保留, 无bfcache.
Chrome 21.0.1180.6
打开页面或则back时都会触发Ready/Load/PageShow事件, 红色未保留, 无bfcache.
Firefox 15.0
打开页面或则back时都会触发Ready/Load/PageShow事件,点击[前往其它网页]会触发PageHide, [back]时会触发PageShow, 红色被保留, 有bfcache.
Safari 5.1.5
打开页面或则back时都会触发Ready/Load/PageShow事件,点击[前往其它网页]会触发PageHide, [back]时会触发PageShow, 红色被保留, 有bfcache.
Safari on iPad (iOS 5.1.1)
打开页面或则back时都会触发Ready/Load/PageShow事件,点击[前往其它网页]会触发PageHide, [back]时会触发PageShow, 红色被保留, 有bfcache.
Opera 12.00
打开页面或则back时都会触发Ready/Load事件, [back]时会触发PageShow, 红色被保留, 有bfcache但不会触发PageShow事件.

总结: Firefox和Safari会bfcache, back时不会触发load, ready事件, 只会触发onpageshow, 而chrome虽然支持onpageshow, 但是back时一样都会触发load,ready事件, opera最操蛋, back时会bfcache,但是不触发onpageshow事件.

回到上面的问题, 如何解决bfcache时ready在back时不执行的问题呢?

起初是想新增一个$.pageshow(), 若浏览器支持, 将业务代码放在onpageshow事件里面处理, 否则用ready处理, 如下:

 $.pageshow = function (fn) {
if (typeof window.onpageshow == "undefined")
$(document).ready(fn);
else
$(window).bind("pageshow", fn);
};
$.pageshow(function () {
alert("Page Show");
alert(typeof window.onpageshow == "undefined")
});

很艹蛋啊, 这个方法只能解决Firefox、Safaer上的问题, 但是在Opera上就没什么效果.

还好在MDC的文档上找到一点思路, Firefox在某些条件下禁用bfcache:

There are instances in which Firefox doesn’t cache pages. Below are some common programmatic reasons that a page is not cached:

the page uses an unload or beforeunload handler;
the page sets "cache-control: no-store".
the site is HTTPS and page sets at least one of:
"Cache-Control: no-cache"
"Pragma: no-cache"
with "Expires: 0" or "Expires" with a date value in the past relative to the value of the "Date" header (unless "Cache-Control: max-age=" is also specified);
the page is not completely loaded when the user navigates away from it or has pending network requests for other reasons (e.g. XMLHttpRequest));
the page has running IndexedDB transactions;
the top-level page contains frames (e.g. <iframe> ) that are not cacheable for any of the reasons listed here;
the page is in a frame and the user loads a new page within that frame (in this case, when the user navigates away from the page, the content that was last loaded into the frames is what is cached).

想了下如果Firefox可以这样, Safari和Chrome应该也可以, 于是找到一个非常简单的方法来解决这个问题, 并且兼容Firefox、Safari、Opera, 只要在页面中加入下面的代码:

$(window).unload(function () { });

经过测试, 页面上一点绑定unload事件, Firefox/Safari/Opera等浏览器变会认为该页面不需要bfcache, 回归到传统的Cache模式, 这样就能解决back时不触发onload, ready等事件带来的问题了.

下面有两个连接, 不过解决问题的思路都差不多.

http://stackoverflow.com/questions/11979156/mobile-safari-back-button

http://stackoverflow.com/questions/24046/the-safari-back-button-problem

浏览器返回按钮不会触发onLoad事件的更多相关文章

  1. HTML5 浏览器返回按钮/手机返回按钮事件监听

    1.HTML5  History对象 支持使用pushState()方法修改地址栏地址,而不刷新页面. popstate事件 当history实体被改变时,popstate事件将会发生.调用pushS ...

  2. event.target 属性返回哪个 DOM 元素触发了事件。

    <ul> <li>list <strong>item 1</strong></li> <li><span>list ...

  3. JS事件 加载事件(onload)注意:1. 加载页面时,触发onload事件,事件写在<body>标签内。 2. 此节的加载页面,可理解为打开一个新页面时。

    加载事件(onload) 事件会在页面加载完成后,立即发生,同时执行被调用的程序. 注意:1. 加载页面时,触发onload事件,事件写在<body>标签内. 2. 此节的加载页面,可理解 ...

  4. IFRAME动态加载触发onload事件(转)

    原文地址:http://blog.ops.cc/webtech/javascript/f5nhm.html <body> <script>var iframe = docume ...

  5. jquery load()函数和window.onload事件

    我想用jquery load()一个饼状图页面, 但是load不出来 代码如下: 后来百度了一下,解决办法如下: window.onload事件只有在文档载入的时候才会执行的,你载入子页面不会触发这个 ...

  6. GridView中的更新按钮不能触发RowUpdating事件

    当点击“编辑”按钮以后,可以看到“更新”和“取消”按钮,“取消”按钮可以正常触发RowCancelingEdit事件,但是“更新”按钮不能触发RowUpdating事件. 解决方案: 在<asp ...

  7. onload事件,解决不能在head写代码

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="con ...

  8. JavaScript 中 onload 事件绑定多个方法的优化建议

    页面加载完毕时会触发 onload 事件.基于内容(HTML)要与行为(JavaScript)分离的编码思想,我们需要将一些对页面的初始化操作写在方法内,并通过window.onload = func ...

  9. js onload事件使用

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

随机推荐

  1. 创建FBI树

    需求:数串由2^n个'0' '1'数串组成,对于一个数串,有01混合出现,则视为F,全0数串为B,全1数串为I. 将给定数串进行切割,如10010011可以用二叉树表示为 F(10010011) / ...

  2. hdu 1166 敌兵布阵_线段树

    题意:略 思路:这题是单点更新,如果是减少的话,直接把数据变成负加上去就行了. #include <iostream> #include<cstdio> #include< ...

  3. logback.xml配置详解

    先附上本文分析用的例子: <?xml version="1.0" encoding="UTF-8" ?> <configuration> ...

  4. magento后台登陆404、Front controller reached 100 router match iterations的解决方案

    (1)执行如下sql SET SQL_SAFE_UPDATES=; SET FOREIGN_KEY_CHECKS=; UPDATE `core_store` SET store_id = WHERE ...

  5. eclipse sysout快捷输入启用

    Window-Preference-java-editor-content assist-advanced 一定要勾选Template Propasals. 关于Template Propasals: ...

  6. Linux之旅(1): diff, patch和quilt (下)

    Linux之旅(1): diff, patch和quilt (下) 2 quilt 我们自己的项目能够用cvs或svn管理所有代码.但有时我们要使用其它开发人员维护的项目.我们须要改动一些文件,但又不 ...

  7. 【递推】【HDU2585】【hotel】

    Hotel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  8. BZOJ 4008: [HNOI2015]亚瑟王( dp )

    dp(i, j)表示考虑了前i张牌, 然后还有j轮的概率. 考虑第i+1张牌: 发动的概率 : p = dp(i, j) * (1 - (1-p[i+1])^j) 没发动的概率 : dp(i, j) ...

  9. sublime text 2中Windows下编写的脚本在Linux平台上运行遇到字符问题

    今天在windows下的sublime text 2下写了个脚本,上传到ubuntu服务器中执行后提示: -bash: /usr/bin/python: /usr/bin/python^M: bad ...

  10. git使用前配置

    1. git config --global user.name "Trey" 2. git config --global user.email 164355949@qq.com ...