Cristian Boariu
Cristian Boariu

Reputation: 9621

why this jQuery does not work in Chrome?

I have a page with a form. There is an input control I populate with the parameter email's value from the url.

So, when the page is loaded, I automatically submit the form to mail chimp, like this:

 <script type="text/javascript">
        $(document).ready(function () {
            $('#mc-embedded-subscribe-form').submit();
            if ($("#previousUrl").val().indexOf('?') != -1) {
                window.location.replace($("#previousUrl").val() + '&emailRegistered=true#newsletterAnchor');
            }
            else {
                window.location.replace($("#previousUrl").val() + '?emailRegistered=true#newsletterAnchor');
            }
        });
    </script>

and immediately after submit, I do a redirect to previous url.

Well, all this works fine in Firefox (a popup for confirmation is also opened after submitting the form and the main page is redirected back to the previous one.

I do not understand why this does not work in Chrome. I mean, somehow the POST action is not finished, because no other popup is opened (altought this page is redirecting back to the initial one).

Can you suggest me a way to do the redirect when FOR SURE the POST has been done? (in this way I think will work in Chrome too)...

UPDATE:

Form's target is "_blank" so the script from mail chimp (that's where I do POST) opens a confirmation popup window.

UPDATE 2:

Tried with ajax but for some reason it does not do anything...

$(document).ready(function () {
            jQuery.ajax({
                url: $('#mc-embedded-subscribe-form').attr('action'),
                data: $('#mc-embedded-subscribe-form').serialize(),
                type: 'POST',
                success: function () {
                    if ($("#previousUrl").val().indexOf('?') != -1) {
                        window.location.replace($("#previousUrl").val() + '&emailRegistered=true#newsletterAnchor');
                    }
                    else {
                        window.location.replace($("#previousUrl").val() + '?emailRegistered=true#newsletterAnchor');
                    }
                }
            });
            return false;
    });

Upvotes: 0

Views: 380

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

You're trying to submit a form and redirect to another page at the same time? You can't do that. You can only make one request at a time (without AJAX, that is).

Upvotes: 1

Related Questions