How dangerous is e.preventDefault();, and can it be replaced by keydown/mousedown tracking?

前端 未结 7 1824
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 17:51

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 accuratel

7条回答
  •  借酒劲吻你
    2021-02-04 18:41

    If you must have forms always work but tracking can be sacrificed if absolutely necessary, you could just try/catch it.

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

提交回复
热议问题