skip to Main Content

I was reading klaviyo’s integration guide. The javascript it is using to submit form data is:

<script type="text/javascript">
    $("input[name='contact[email]']").on('blur', function(e) {
        e.preventDefault();
        var email = $(this).val();
        var settings = {
            "async": true,
            "crossDomain": true,
            "url": "https://manage.kmail-lists.com/subscriptions/external/subscribe",
            "method": "POST",
            "headers": {
                "content-type": "application/x-www-form-urlencoded",
                "cache-control": "no-cache"
            },
            "data": {
                "g": "LIST_ID",
                "$fields": "$source",
                "email": email,
                "$source": "Shopify form"
            }
        };
        $.ajax(settings).done(function (response) {
            console.log(response);
        });
    });
</script>

I am wondering why it is using the “blur” event instead of user’s clicking on “submit” button to trigger the ajax function. I think with this code, if user opt out of the form’s text-box prematurely without clicking “sign up” or “submit”, the data will be submitted.

2

Answers


  1. The blur event fires when the element loses focus and potentially ‘after’ the email is typed in it gets sent. This is helpful with conversions from dropoffs in general.

    Login or Signup to reply.
  2. “I am wondering why it is using the “blur” event instead of user’s clicking on “submit” button to trigger the ajax function.”

    Because this two ways will give you two different behaviors. Very simple:

    1. When you use click event, the function will be executed after button click
    2. When you use blur event, the function will be executed after the input will lose it’s focus
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search