onbeforeunload谷歌浏览器,lodop 谷歌浏览器
大家好,感谢邀请,今天来为大家分享一下onbeforeunload谷歌浏览器的问题,以及和lodop 谷歌浏览器的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!
本文目录
一、...浏览器事件之chrome浏览器真支持onbeforeunload事件吗
背景:做Flash关闭时做下统计视频的浏览数,想发个请求给服务器+1,Firefox,IE9,(IE8不行)都行,再就是Chrome不行,如下备案。
1、window.onbeforeunload()函数主要是用于捕获关闭浏览器事件(包括刷新);
2、window.onunload()函数主要是执行关闭游览器后的动作;
window.attachEvent("onbeforeunload",this.tracePlay);
window.addEventListener("beforeunload",this.tracePlay,false);
if($('#__XYFlashPlayer__')!= null){
var playTraceerImg= document.getElementById("playTraceerImg");
playTraceerImg.src=$('#__XYFlashPlayer__').get(0).getStaticData()+"&rand="+Math.random();
<{include file="admin/footer.html"}>
在这个footer.html里包含的是另一个js的域名:(较大网站都这么干,程序和css,js分开以分摊服务器的压力)
在footer.html这个模板里有如下js,分析这个staticURL:
<script src="<{$staticURL}>js/justwinit.common.js?ver=<{$version}>" type="text/javascript"></script>
preview.php把这个配置给render到smarty模板里:
$this->view->staticURL= KO::config('url.static_url');
url.php里配置该静态文件的域名,这个在apache里配置好,下面会有示例:
'static_url'=>';,
# ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot"D:\www\justwinit\trunk\assets\static"
ErrorLog"logs/w.justwinit.cn-error.log"
CustomLog"logs/w.justwinit.cn-access.log" common
<Directory"D:/www/justwinit/trunk/assets/static/">
Options Indexes FollowSymLinks MultiViews
用普通的js无法实现在兼容监听IE,FF,Google等浏览器的关闭事件。经过测试,用jq是可以实现兼容的,不过并不保证完全兼容,还需要你自己测试一下,只需一句简短的语句就可以至少兼容三大浏览器了:
<script type="text/javascript">
window.onbeforeunload= function(){return'Sure to leave?';};
chrome浏览器支持onbeforeunload事件吗?
Chrome Safari在调用 document.write、document.open、document.close方法以及"javascipt:"伪协议时,不会触发 onbeforeunload事件。
是bug,见
用的时候chrome并不支持onbeforeunload。
window.onbeforeunload=function(){...}不执行其中的代码
$(window).bind('beforeunload', function(){
Works great with Firefox, IE8 but not in Chrome. Is it a known problem or is there any alternative for that?
Actually what I am trying to do is to log details whenever user tries to close the browser.
$(window).bind('beforeunload', function(){
This works well in Firefox, but not in Chrome
From:
老外说的Ajax,我也试了,也确实不行的,测试下其他的方法,当写成这样:
window.onbeforeunload= function(){
console.log("Helo test chrome beforeunload");
<body onunload="alert('Helo test chrome unload')">
里面有Alert这些输出时,会有如下提示:
Blocked alert('Helo test chrome beforeunload')。
Blocked alert('Helo test chrome unload') during unload。
<iframe id="iframedemo" name="iframedemo" src="inner.html" width="10%" frameborder="1"></iframe>
<script language="javascript">
问题依旧提示有问题,嗨,怎么办呢?
<script src=""></script>
<script>!window.jQuery&& document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')
<script language="javascript">
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定。区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行。
Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读取新的页面时调用,此时还没开始读取;而onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用。Onunload是无法阻止页面的更新和关闭的。而 Onbeforeunload可以做到。
window关闭时onunload,onbeforeunload处理
要想在页面跳转时询问用户,需要在onbeforeunload事件中返回询问字符:
window.onbeforeunload= function(e){
return‘Are You Sure To Leave This Page?’;
如果在关闭页面时需要做些请求动作,在onunload事件中处理较好:
1:不要试图用addEventListener或attachEvent绑定这两个事件,浏览器不兼容。
2:应该在onbeforeunload中询问,而将退出动作放在onunload中,这样逻辑好清晰。
3:如果是ajax请求放在onunload事件中,需要同步执行ajax,否则是不能保证这个ajax请求会成功的。
二、怎么判断是手动刷新浏览器还是程序刷新浏览器
1、一、目前搜出来比较普遍的根据beforunload以及unload执行的间隔时间
2、window.onbeforeunload=function(e){//执行该事件时将当前时间存入cookie
3、 this.$cookies.set('_onbeforeTime',new Date().getTime());//格式:名称,值,时长(可不填)
4、 this._unloadTime=new Date().getTime();
5、 this._onbeforeTime=this.$cookies.get('_onbeforeTime');//读取cookie值
6、 this._gap_time=this._unloadTime-this._onbeforeTime;//两个事件执行间隔时间
7、/**判断间隔时间,根据个人浏览器性能而定,需要进行测试。
8、/*这里测试出的数据关闭浏览器是在0~8ms
9、/*不同浏览器数据不同,火狐在关闭浏览器时时间差在0~20ms,刷新则在15ms以上
10、 if(!this.isLogin) window.localStorage.clear();
11、 this.$cookies.remove()//清除cookie缓存
12、由于不同浏览器之间两个方法的执行间隔时间不同,因此该方法会不太稳定,建议多测试找到一个稳定的间隔数值
13、二、原理同上,根据unload以及load的间隔时间判断
14、一般来说,在刷新时两个方法是紧接着执行的,间隔时间相较于重新打开浏览器而言会小很多,同样的,间隔时间需要多做测试取准确值,此方法相较于第一个方法而言,准确度会高很多
15、window.onunload=function(e){//执行该事件时将当前时间存入cookie
16、 this.$cookies.set('_onbeforeTime',new Date().getTime());//格式:名称,值,时长(可不填)
17、 this._unloadTime=new Date().getTime();
18、 this._onbeforeTime=this.$cookies.get('_onbeforeTime');//读取cookie值
19、 this._gap_time=this._unloadTime-this._onbeforeTime;//两个事件执行间隔时间
20、/**判断间隔时间,需要进行测试。
21、/*这里测试出的数据关闭浏览器是在10000ms以上
22、 if(this._gap_time> 10000){
23、 if(!this.isLogin) window.localStorage.clear();
24、 this.$cookies.remove()//清除cookie缓存
25、三、根据window.performance.navigation.type属性判断(该属性返回一个整数值,表示网页的加载来源,可能有以下4种情况):
26、0:网页通过点击链接、地址栏输入、表单提交、脚本操作等方式加载,相当于常数performance.navigation.TYPE_NAVIGATE。
27、1:网页通过“重新加载”按钮或者location.reload()方法加载,相当于常数performance.navigation.TYPE_RELOAD。
28、2:网页通过“前进”或“后退”按钮加载,相当于常数performance.navigation.TYPE_BACK_FORWARD。
29、255:任何其他来源的加载,相当于常数performance.navigation.TYPE_RESERVED。
30、四、在window上添加属性进行区分是否被刷新或者是第一次加载,即使页面刷新, name属性值也不会被重置,除非手动关闭当前页面
31、五、(只限于谷歌浏览器)根据window.chorme.loadTime().navigation
三、在js中,浏览器关闭还是刷新应该怎么判断
页面刷新时先执行onbeforeunload,然后onunload,最后onload。
//页面刷新时,刷新之前执行onbeforeunload事件,在新页面即将替换旧页面时onunload事件,最后onload事件。
//页面关闭时,先onbeforeunload事件,再onunload事件。
//页面刷新时,只执行onunload;页面关闭时,只执行onbeforeunload事件
那么回归正题,到底怎样判断浏览器是关闭还是刷新?我按照网上的各种说法实验千百遍,都未成功,其中各种说法如下:
window.onbeforeunload= function()//author: meizz
var n= window.event.screenX- window.screenLeft;
var b= n> document.documentElement.scrollWidth-20;
if(b&& window.event.clientY< 0|| window.event.altKey)
window.event.returnValue="";//这里可以放置你想做的操作代码
window.onbeforeunload= function()//author: meizz
var n= window.event.screenX- window.screenLeft;
var b= n> document.documentElement.scrollWidth-20;
if(b&& window.event.clientY< 0|| window.event.altKey)
window.event.returnValue="";//这里可以放置你想做的操作代码
if(event.clientX<=0&& event.clientY<0){
这些方法都不管用,但是我并没有放弃,想啊想啊........
//页面刷新时,刷新之前执行onbeforeunload事件,在新页面即将替换旧页面时onunload事件,最后onload事件。
//页面关闭时,先onbeforeunload事件,再onunload事件。
//页面刷新时,只执行onunload;页面关闭时,只执行onbeforeunload事件
刷新的时候先onbeforeunload,然后取服务端请求数据,在新页面即将替换旧页面时onunload事件,而页面关闭时,先onbeforeunload事件,再立即onunload事件。那么在刷新的时候,onbeforeunload与onunload之间的时间肯定比关闭的时候时间长,经过测试确实如此。
var _beforeUnload_time= 0, _gap_time= 0;
var is_fireFox= navigator.userAgent.indexOf("Firefox")>-1;//是否是火狐浏览器
_gap_time= new Date().getTime()- _beforeUnload_time;
$.post(pathName+"/back/bi!aaaa.s2?t="+_beforeUnload_time,{msg:"浏览器关闭",time:_gap_time},function(json){},"text");
$.post(pathName+"/back/bi!aaaa.s2?t="+_beforeUnload_time,{msg:"浏览器刷新",time:_gap_time},function(json){},"text");
window.onbeforeunload= function(){
_beforeUnload_time= new Date().getTime();
$.post(pathName+"/back/bi!aaaa.s2?t="+_beforeUnload_time,{msg:"火狐关闭"},function(json){},"text");
END,本文到此结束,如果可以帮助到大家,还望关注本站哦!
声明:信息资讯网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,版权归原作者东方体育日报所有。若您的权利被侵害,请联系 删除。
本文链接:http://www.gdxhedu.com/news/106334.html