简体   繁体   English

如何准确找出引发事件的原因?

[英]How to find out precisely what triggered the event?

I'm using some function with jQuery, for example: 我在jQuery中使用了一些功能,例如:

$(window).one('beforeunload', function() {
//my code
});

But I really want to find out all about what has triggered this event and how. 但我真的很想了解引发此事件的原因和方式。

I tried the following code: 我尝试了以下代码:

$(window).one('beforeunload', function(e) {
alert(JSON.stringify(e));
});

But it raises exception about circular structure. 但是它提出了关于圆形结构的例外。 Definitely though there must be some other way of differentiating and debugging through it. 当然,必须有其他方法可以通过它进行区分和调试。

How am I going to apply it? 我要如何申请?

I want to see what triggered this event, was it a link, a submitted form, a javascript, a page refresh, or a back and forth buttons or whatever, and it's all fine in that case. 我想看看是什么触发了此事件,是链接,提交的表单,javascript,页面刷新还是来回按钮等等,在这种情况下一切都很好。 BUT if it was triggered by a call to open an external application, I want to discard the event and return null. 但是,如果它是由打开外部应用程序的调用触发的,我想放弃该事件并返回null。

Example of external application launch: 外部应用程序启动的示例:

<a href="skype:your_skype_name?call" >

Any ideas? 有任何想法吗?

SOLVED 解决了

This is how I solved it. 这就是我解决的方法。

I just make some script link first, to gain more control over the event: 我只是先建立一些脚本链接,以获取对该事件的更多控制权:

<a href="javascript:void(0)" id="skype_click_ok"

Then I create a global variable to store the state of beforeunload: 然后,我创建一个全局变量来存储beforeunload的状态:

var dontunload=0;

Then I manually process the link, but setting location.href, and changing variable state: 然后,我手动处理链接,但是设置location.href并更改变量状态:

$('#skype_click_ok').click(function(){
dontunload=1;
location.href='skype:marilynbrusas?call';
});

Now my actual beforeunload event, note the one() was also changed to bind() : 现在我实际的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 EDIT3

Actually it's not solved. 其实还没有解决。 This trick does not work in IE and also the even triggers from javascript:void(0) links... 这个技巧在IE中不起作用,javascript:void(0)链接中的偶数触发器也无法使用...

You can use the event.target.id selector to pass what element triggered the event, like so: 您可以使用event.target.id选择器传递触发事件的元素,如下所示:

$(document).ready(function() {
    $("a").click(function(event) {
        alert(event.target.id);
    });
});

See jsfiddle . 参见jsfiddle As the commentor mentioned, this will alert the ID of the element used to select, I just used that as an example as you didn't fully state what you wanted retrieved? 正如评论员提到的那样,这将提醒用于选择的元素的ID,我只是以它为例,因为您没有完全说明要检索的内容?

Using alert to display objects like events is fairly useless. 使用alert来显示事件之类的对象是毫无用处的。

Why don't you try sending you event to the console ? 为什么不尝试将事件发送到控制台

function(event) {
    console.log(event);
}

This way you can expand you event object and view it in aa tree like structure. 这样,您可以展开事件对象并以树状结构查看它。

EDIT As per you comments about the location of the event trigger: I haven't testing this, but this could be a way of stopping your unload event. 编辑根据您对事件触发器的位置的评论:我尚未对此进行测试,但这可能是停止卸载事件的一种方式。

  • Listen for click events for all a elements 监听所有点击事件的a元素
  • Check the element clicked points to an external application (doesn't start with http://) 检查单击的元素是否指向外部应用程序(不是以http://开头)
  • If not, bind an event handler for beforeunload that cancels the event immediately 如果不是,请为beforeunload绑定事件处理程序,以立即取消事件
  • Otherwise unbind the previous handler 否则,取消绑定先前的处理程序

     $("a").click(function(event) { if(event.target.href.indexOf("http://") != 0) { $(window).on("beforeunload.override", function(e) { // Stop the 'beforeunload' event. return false; } } else { $(window).off("beforeunload.override"); } } 

Notice that this example is using something call Event Namespacing . 请注意,此示例使用的是所谓的Event Namespacing By specifying that we want to listen to the beforeunload event, but we have our own namespace called override , we can add and remove the event handler safely without removing all other listeners for the beforeunload event. 通过指定我们要侦听beforeunload事件,但我们拥有自己的命名空间override ,我们可以安全地添加和删除事件处理程序,而无需删除beforeunload事件的所有其他侦听器。

If you want to know everything about the event, you can follow the advise of @Aesthete 如果您想了解有关活动的所有信息,可以按照@Aesthete的建议进行。

$('#id').click(function(e){
         console.log(e)
          });

Then you can review the Console and inspect the output event. 然后,您可以查看控制台并检查输出事件。

Using target="_blank" solves this problem as well: 使用target="_blank"可以解决此问题:

<a href="skype:your_skype_name?call" target="_blank">

It's rather ugly, but in my case it was the only solution that flawlessly worked across all browsers. 这很丑陋,但就我而言,这是唯一可在所有浏览器上完美运行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM