Getting a callback function to trigger after clicking on an alert with JQuery
<p>I have a callback function which redirects the user and I want to have it trigger after clicking "OK" on an alert window. Right now, the code looks like this:</p>

<pre><code>function myFunction(callback) {
    var ajaxUrl = "url_path";
    $.ajax({
        url: ajaxUrl,
        success: function (data) {
            if (data.fieldIsTrue) {
                alert("You are being redirected", callback());
            } else {
                callback();
            }
        }
    });
}
</code></pre>

<p>Where the callback contains a redirect and form submission. The callback goes to a different location based on the view model fields, so the callback shouldn't be different.  </p>

<p>The issue is that the callback in the alert is being called once the alert pops up and not when the alert is clicked away. Is there anyway I can get the alert window to stay up, either between pages or make the callback execute once the alert disappears?  </p>

<p>Edit: Origionally, I had tried using a success function that looked like this:  </p>

<pre><code>success: function (data) {
            if (data.fieldIsTrue) {
                alert("You are being redirected", callback());
            }
                callback(); 
        }
</code></pre>

<p>So I don't think the solution is   </p>

<pre><code>alert();
callback();
</code></pre>

<p>Edit2: just tried doing the   </p>

<pre><code>alert();
callback();
</code></pre>

<p>and confirmed that the redirect still occurs before clicking on the pop up  </p>

<p><strong>Here is more information since the problem is somewhere else:</strong>  </p>

<p>The function call with callback:</p>

<pre><code>        $(".button").click(function (e) {
        //e.preventDefault(); doesn't seem to help
        myFunction(function () {
            if ($("#myForm").validate().form()) {
                $("button").disable();
                document.forms['myForm'].submit();
            } else {
                ValidateFields();
            }
        });  
</code></pre>

<p>Some of the cshtml:   </p>

<pre><code>@{Html.BeginForm&lt;MyController&gt;(x =&gt; x.FunctionWithRedirects(null), FormMethod.Post, new { id = "myForm" });}  
//some hidden fields
&lt;button class="button myButton"&gt;Button I want to alert from&lt;/button&gt;
@{Html.EndForm();}  
</code></pre>

<p><strong>Solution:</strong> So the final working solution I have actually has the original function, <em>however</em> it appears that I did not have e.preventDefault() in the original button click initialization, so the form would still submit. It was an oversight on my part, and thanks to everyone who tried to help.</p>