e.preventDefault();有多危险,可以用keydown / mousedown跟踪代替它? [英] How dangerous is e.preventDefault();, and can it be replaced by keydown/mousedown tracking?

查看:145
本文介绍了e.preventDefault();有多危险,可以用keydown / mousedown跟踪代替它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个相当复杂的CRM跟踪脚本,用于跟踪Google Analytics中的表单操作。我试图平衡追踪表单操作与需要的准确性,以防止表单无法正常工作。



现在,我知道这样做是行不通的。

 <$ c $($'this').attr('action')] $($''''')'提交(函数(){
_gaq.push(['_ trackEvent','Form','Submit' ;
});

在有机会处理之前,DOM卸载。



所以,很多示例代码都提供了如下的代码:

  $('form')。submit函数(e){
e.preventDefault();
var form = this;
_gaq.push(['_ trackEvent','Form','Submit',$(this))。 attr('action')]);
//...do一些其他跟踪的东西...
setTimeout(function(){
form.submit();
},400);
});

这在,但这让我感到紧张。如果在 e.preventDefault(); 之间发生什么事情,并且当我开始触发基于DOM的提交?我完全打破了表单。



我一直在讨论一些其他的分析实现,并且我注意到了类似这样的东西

  $('form ').mousedown(function(){
_gaq.push(['_ trackEvent','Form','Submit',$(this).attr('action')]);
}) ;如果keydown是输入键
_gaq.push([
$('form')。keydown(function(e){
if(e.which === 13) '_trackEvent','Form','Submit',$(this).attr('action')]);
});

基本上,不是中断表单提交,而是通过假设某人正在喋喋不休或键入按Enter键,比表单提交。显然,这会导致一定数量的误报,但它完全消除了使用 e.preventDefault(); ,这在我的脑海中消除了我可能永远存在的风险阻止表单成功提交。

所以,我的问题是:


  • 是否有可能将标准
    表格跟踪代码段并阻止它从 ever
    完全阻止表单
    提交?


  • mousedown / keydown是否可行?

  • 是否有任何可能会遗漏的提交情况?具体来说,除了鼠标和键盘输入外,还有其他方式可以提交吗?在开始卸载页面之前,浏览器是否总是有时间处理JavaScript?
  • 他们因为那里Googleplex是非常光明的,他们总有一天在想这件事一定会发生,现在肯定会发生。为什么不给这个好试试:

      $('form')。submit(function(e){
    e.preventDefault();
    var form = this;
    _gaq.push(['_ trackEvent','Form','Submit',$(this).attr('action') ]);
    //...do一些其他跟踪的东西...
    _gaq.push(function(){
    form.submit();
    });
    });

    _gaq.push thigamajigger执行其元素所以你应该开玩笑。



    不,我不知道为什么我突然开始这样说。


    I'm working on a tracking script for a fairly sophisticated CRM for tracking form actions in Google Analytics. I'm trying to balance the desire to track form actions accurately with the need to never prevent a form from not working.

    Now, I know that doing something like this doesn't work.

    $('form').submit(function(){
     _gaq.push(['_trackEvent', 'Form', 'Submit', $(this).attr('action')]);
    });
    

    The DOM unloads before this has a chance to process.

    So, a lot of sample code recommends something like this:

    $('form').submit(function(e){
    e.preventDefault();
    var form = this; 
     _gaq.push(['_trackEvent', 'Form', 'Submit', $(this).attr('action')]);
    //...do some other tracking stuff...
    setTimeout(function(){
    form.submit();
    }, 400);
    });
    

    This is reliable in most cases, but it makes me nervous. What if something happens between e.preventDefault();and when I get around to triggering the DOM based submit? I've totally broken the form.

    I've been poking around some other analytics implementations, and I've noticed something like this:

    $('form').mousedown(function(){
     _gaq.push(['_trackEvent', 'Form', 'Submit', $(this).attr('action')]);
    });
    $('form').keydown(function(e){
        if(e.which===13) //if the keydown is the enter key
        _gaq.push(['_trackEvent', 'Form', 'Submit', $(this).attr('action')]);
    });
    

    Basically, instead of interrupting the form submit, preempting it by assuming that if someone is mousing down or keying down on Enter, than that form is submitted. Obviously, this will result in a certain amount of false positives, but it completely eliminates use of e.preventDefault();, which in my mind eliminates the risk that I might ever prevent a form from successfully submitting.

    So, my question:

    • Is it possible to take the standard form tracking snippet and prevent it from ever fully preventing the form from submitting?
    • Is the mousedown/keydown alternative viable?
    • Are there any submission cases it may miss? Specifically, are there other ways to end up submitting besides the mouse and the keyboard enter? And will the browser always have time to process javascript before beginning to unload the page?

    解决方案

    Them fellers over at that there Googleplex are awful bright and they figgered some day somethin' like this was bound to happen and now, sure enough, it has. Why don't you give this a good try:

    $('form').submit(function(e){
      e.preventDefault();
      var form = this; 
      _gaq.push(['_trackEvent', 'Form', 'Submit', $(this).attr('action')]);
      //...do some other tracking stuff...
      _gaq.push(function(){
         form.submit();
        });
      });
    

    That _gaq.push thigamajigger executes its elements sequentially, so you should be jest fine.

    And no, I don't know why I suddenly started talking like this.

    这篇关于e.preventDefault();有多危险,可以用keydown / mousedown跟踪代替它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆