我使用的一些功能使用jQuery,例如:如何准确找出触发事件的原因?
$(window).one('beforeunload', function() {
//my code
});
但我真的想找到所有关于什么触发此事件,以及如何。
我尝试下面的代码:
$(window).one('beforeunload', function(e) {
alert(JSON.stringify(e));
});
但它引起了人们对圆形结构异常。当然,必须通过它来区分和调试。
我怎么申请呢?
我希望看到什么触发此事件,是一个链接,提交的表单,一个javascript,页面刷新,或来回按钮或什么的,并且它的所有罚款在这种情况下。 但,如果它是通过调用触发打开外部应用程序,我想放弃该事件并返回null。
外部应用程序启动的实例:
<a href="skype:your_skype_name?call" >
任何想法?
解决
这是我如何解决它。
我只是做了一些脚本的链接首先,在事件以获得更多的控制权:
<a href="javascript:void(0)" id="skype_click_ok"
然后,我创建一个全局变量来存储beforeunload的状态:我手动
var dontunload=0;
然后处理链接,但设置location.href,并更改变量状态:
$('#skype_click_ok').click(function(){
dontunload=1;
location.href='skype:marilynbrusas?call';
});
现在我致动人beforeunload事件,请注意one()
也改为bind()
:
$(window).bind('beforeunload', function() {
if (dontunload==1) dontunload=0; //if triggered from our skype - do nothing, yet cancel state for the next call
else { //Do the actual code
// Fade out, before leaving the page.
$('html#my_html').stop(true,false).css('overflow','hidden').animate({opacity:0},2000);
// Lock content with invalid image hack
$('body#my_body').prepend(
$('<div>').attr({style:'position:fixed;height:100%;width:100%;left:0;top:0;z-index:900100;background-image:url(in-your-face)'})
);
// unbind the event
$(this).unbind('beforeunload');
}
});
EDIT3
其实它没有解决。这一招并不在IE浏览器,也是甚至从JavaScript触发:无效(0)链接...
打开外部应用程序的调用?请你能更好地解释一下吗? –
'' - 这确实会触发GET,因此会触发before-unload事件。外部应用程序意味着它有一个内部协议,而不是'http:',例如'skype:'。所以它会尝试启动一个Skype应用程序,而不是在网络上打开一些页面。如果是这样的话,我确实想取消我的活动。 – Anonymous