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

前端 未结 7 1830
隐瞒了意图╮
隐瞒了意图╮ 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:40

    e.preventDefault() doesn't have to be right at the beginning of the function call. Why not just have an if statement to verify if everything is working correctly, and only call e.preventDefault() if it does. If any function in the chain doesn't return the expected result, set a submitted variable to false, and don't prevent the default.

    This might be a little more difficult to handle when it comes to anything asynchronous (such as your setTimeout, but there will be ways to make fairly sure, depending on what your code looks like. So you can check if methods exist with if (_gaq.push) (for example). You can't make 100% sure without testing every single combination in every browser, but I reckon you can get a pretty satisfactory result with this.

提交回复
热议问题