<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[JS捕获关闭浏览器事件之chrome浏览器真支持onbeforeunload事件吗？]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Mon, 04 Mar 2013 03:32:22 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：做Flash关闭时做下统计视频的浏览数，想发个请求给服务器+1，Firefox，IE9，（IE8不行）都行，再就是Chrome不行，如下备案。<br/>常常的网上结论是这样的：<br/>1、window.onbeforeunload()函数主要是用于捕获关闭浏览器事件（包括刷新）；<br/>2、window.onunload()函数主要是执行关闭游览器后的动作；<br/><br/>实践中听说firefox有些问题：<br/><textarea name="code" class="JS" rows="15" cols="100">
function wisTracker()&#123;
&nbsp;&nbsp;this.bindClick = function()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(document.attachEvent)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; window.attachEvent(&quot;onbeforeunload&quot;,this.tracePlay);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; window.addEventListener(&quot;beforeunload&quot;,this.tracePlay,false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;
&nbsp;&nbsp;&#125;;&nbsp;&nbsp;
&nbsp;&nbsp;this.tracePlay = function()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if($(&#039;#__XYFlashPlayer__&#039;) != null)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var playTraceerImg = document.getElementById(&quot;playTraceerImg&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playTraceerImg.src = $(&#039;#__XYFlashPlayer__&#039;).get(0).getStaticData()+&quot;&amp;rand=&quot;+Math.random();
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;;
&#125;
</textarea><br/>在footer.html里调用：<br/><textarea name="code" class="JS" rows="15" cols="100">
wisTracker = new wisTracker();
wisTracker.bindClick();
</textarea><br/>在每个访问的页面里包含：<br/><br/><textarea name="code" class="JS" rows="15" cols="100">&lt;&#123;include file=&quot;admin/footer.html&quot;&#125;&gt;</textarea><br/><br/>在这个footer.html里包含的是另一个js的域名：（较大网站都这么干，程序和css，js分开以分摊服务器的压力）<br/>在footer.html这个模板里有如下js，分析这个staticURL：<br/>&lt;script src=&quot;&lt;&#123;$staticURL&#125;&gt;js/justwinit.common.js?ver=&lt;&#123;$version&#125;&gt;&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br/>preview.php 把这个配置给render到smarty模板里：<br/>$this-&gt;view-&gt;staticURL = KO::config(&#039;url.static_url&#039;);<br/>url.php里配置该静态文件的域名，这个在apache里配置好,下面会有示例：<br/>&#039;static_url&#039;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&#039;http://s.jackxiang.com/&#039;,<br/>Apache配置域名：<br/><textarea name="code" class="C" rows="15" cols="100">
&lt;VirtualHost *:80&gt;
#&nbsp;&nbsp;&nbsp;&nbsp;ServerAdmin webmaster@dummy-host2.example.com
&nbsp;&nbsp;&nbsp;&nbsp;DocumentRoot &quot;D:&#92;www&#92;justwinit&#92;trunk&#92;assets&#92;static&quot;
&nbsp;&nbsp;&nbsp;&nbsp;ServerName s.jackxiang.com
&nbsp;&nbsp;&nbsp;&nbsp;ErrorLog &quot;logs/w.jackxiang.com-error.log&quot;
&nbsp;&nbsp;&nbsp;&nbsp;CustomLog &quot;logs/w.jackxiang.com-access.log&quot; common
&nbsp;&nbsp;&lt;Directory &quot;D:/www/justwinit/trunk/assets/static/&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;Options Indexes FollowSymLinks MultiViews
&nbsp;&nbsp;&nbsp;&nbsp;AllowOverride all
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Order Deny,Allow
&nbsp;&nbsp;Deny from all
&nbsp;&nbsp;Allow from all
&lt;/Directory&gt;
&lt;/VirtualHost&gt;
</textarea><br/><br/>用普通的js无法实现在兼容监听IE,FF,Google等浏览器的关闭事件。 经过测试，用jq是可以实现兼容的，不过并不保证完全兼容，还需要你自己测试一下，只需一句简短的语句就可以至少兼容三大浏览器了：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;script type=&quot;text/javascript&quot;&gt;
window.onbeforeunload = function() &#123;return &#039;Sure to leave?&#039;;&#125;;
&lt;/script&gt;
</textarea><br/><br/>但：<br/>chrome浏览器支持onbeforeunload事件吗？<br/>Chrome Safari 在调用 document.write、document.open、document.close 方法以及 &quot;javascipt:&quot; 伪协议时，不会触发 onbeforeunload 事件。<br/><br/>http://w3help.org/zh-cn/causes/BX2047<br/><br/>是bug，见http://code.google.com/p/chromium/issues/detail?id=4422<br/><br/>用的时候chrome并不支持onbeforeunload。 <br/>window.onbeforeunload=function()&#123;...&#125;不执行其中的代码 <br/><br/>$(window).bind(&#039;beforeunload&#039;, function()&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Good Bye&quot;)<br/>&#125;);<br/>Works great with Firefox, IE8 but not in Chrome. Is it a known problem or is there any alternative for that ?<br/><br/>Actually what I am trying to do is to log details whenever user tries to close the browser.<br/><br/>function LogTime()<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;jQuery.ajax(&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: &quot;POST&quot;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url: &quot;log.php&quot;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data: &quot;&quot;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cache: false,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success: function(response)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;);<br/>&#125;<br/>$(window).bind(&#039;beforeunload&#039;, function()&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;LogTime();<br/>&#125;);<br/>This works well in Firefox, but not in Chrome<br/><br/>From:http://stackoverflow.com/questions/10680544/beforeunload-chrome-issue<br/><br/>老外说的Ajax，我也试了，也确实不行的，测试下其他的方法，当写成这样：<br/>window.onbeforeunload = function() &#123;<br/>&nbsp;&nbsp;console.log(&quot;Helo test chrome beforeunload&quot;);<br/>&nbsp;&nbsp;callExternal();<br/>&#125;;<br/>或：<br/>&lt;body&nbsp;&nbsp;onunload=&quot;alert(&#039;Helo test chrome unload&#039;)&quot;&gt;<br/>里面有Alert这些输出时，会有如下提示：<br/>Blocked alert(&#039;Helo test chrome beforeunload&#039;)。<br/>Blocked alert(&#039;Helo test chrome unload&#039;) during unload。<br/><br/>最后使用Iframe调用的方法：<br/>加入：<br/>&lt;iframe id=&quot;iframedemo&quot; name=&quot;iframedemo&quot; src=&quot;inner.html&quot; width=&quot;10%&quot; frameborder=&quot;1&quot;&gt;&lt;/iframe&gt;&nbsp;&nbsp;<br/>inner.html:<br/>&lt;script language=&quot;javascript&quot;&gt;<br/>function demofunction() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;console.log(&quot;1222&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;demofunction&quot;);<br/>&#125;<br/>&lt;/script&gt;<br/>问题依旧提示有问题，嗨，怎么办呢？<br/>在IFrame用Ajax也不行：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;!window.jQuery &amp;&amp; document.write(&#039;&lt;script src=&quot;js/jquery-1.4.2.min.js&quot;&gt;&lt;&#92;/script&gt;&#039;)  
&lt;/script&gt;

&lt;script language=&quot;javascript&quot;&gt;
function demofunction() &#123;

&nbsp;&nbsp;&nbsp;&nbsp;jQuery.ajax(&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: &quot;POST&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url: &quot;log.php&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data: &quot;&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cache: false,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success: function(response)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;);

&#125;
&lt;/script&gt;
</textarea><br/><br/><br/>Onunload与Onbeforeunload <br/>Onunload，onbeforeunload都是在刷新或关闭时调用，可以在&lt;script&gt;脚本中通过window.onunload来指定或者在&lt;body&gt;里指定。区别在于onbeforeunload在onunload之前执行，它还可以阻止onunload的执行。 <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;Onbeforeunload也是在页面刷新或关闭时调用，Onbeforeunload是正要去服务器读取新的页面时调用，此时还没开始读取；而onunload则已经从服务器上读到了需要加载的新的页面，在即将替换掉当前页面时调用。Onunload是无法阻止页面的更新和关闭的。而 Onbeforeunload 可以做到。<br/><br/>http://blog.csdn.net/victor16345/article/details/7670464<br/><br/><br/>window关闭时onunload,onbeforeunload处理<br/>要想在页面跳转时询问用户，需要在onbeforeunload 事件中返回询问字符:<br/>window.onbeforeunload = function(e) &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ‘Are You Sure To Leave This Page?’; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;;<br/>如果在关闭页面时需要做些请求动作，在onunload事件中处理较好:<br/>window.onunload = function() &#123; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//close function <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;;<br/>注意事项：<br/>1:不要试图用addEventListener或attachEvent绑定这两个事件，浏览器不兼容。<br/>2:应该在onbeforeunload 中询问，而将退出动作放在onunload 中，这样逻辑好清晰。<br/>3:如果是ajax请求放在onunload 事件中，需要同步执行ajax,否则是不能保证这个ajax请求会成功的。
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] JS捕获关闭浏览器事件之chrome浏览器真支持onbeforeunload事件吗？]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>