javascript not executing after ajax post

Last post 08-12-2009 7:40 AM by dotnethacker. 7 replies.

Sort Posts:

  • javascript not executing after ajax post

    06-25-2009, 5:08 PM
    • Member
      point Member
    • JamesDalton
    • Member since 06-25-2009, 8:56 PM
    • Posts 5

    I'm working on a prototype that makes heavy use of partial views and jQuery. When I use jQuery to do the post and updates to the page my javascript fires as I would expect. If i let Ajax.BeginForm handle it the javascript doesn't execute. An example partial view would be

    ...

    <script type="text/javascript">
    $("#FormEdit").ready(function() {
    $("#DateOfBirth").mask("99/99/9999");
    $("#EffectiveDate").mask("99/99/9999");
    $("#TerminationDate").mask("99/99/9999");
    $("#SSN").mask("999-99-9999");
    });
    </script>
    <% using (Ajax.BeginForm("Save", "Individual", new AjaxOptions { UpdateTargetId = "editContent"}))
    {%>

    ...

    The form is initially loaded using jQuery and the masks work. When the partial view is updated after the post, the ready function doesn't get executed. If I create an OnComplete handler and update the page using jQuery it works. Not sure if I'm doing something wrong or if it is a bug/feature of MVC Ajax.


    James

  • Re: javascript not executing after ajax post

    06-25-2009, 9:35 PM
    • Member
      3 point Member
    • lvzhq
    • Member since 06-25-2009, 9:17 AM
    • Posts 7

     Pls check your IIS version, if iis version is low some jquery function is invalid

  • Re: javascript not executing after ajax post

    06-26-2009, 8:25 AM
    • Member
      point Member
    • JamesDalton
    • Member since 06-25-2009, 8:56 PM
    • Posts 5

    This is happening running from VS2008.  jQuery behaves as I would expect it's MVC that is acting strange.

  • Re: javascript not executing after ajax post

    06-26-2009, 10:02 AM
    Answer
    • Contributor
      6,620 point Contributor
    • paul.vencill
    • Member since 02-01-2006, 12:57 PM
    • Gaithersburg, MD
    • Posts 1,350

     there's at least three other posts about this recently...  It's simple; the 'ready'  function fires only on the initial page load, when elements are ready, but before images are done downloading.  It simply doesn't (and isn't designed to) fire after an ajax post.  That's what oncomplete handlers are for, as you've discovered.

    Help those who have helped you... remember to "Mark as Answered"
  • Re: javascript not executing after ajax post

    06-26-2009, 11:32 AM
    • Member
      point Member
    • JamesDalton
    • Member since 06-25-2009, 8:56 PM
    • Posts 5

    From what I've read about the ready function, if the document isn't ready the function gets queued and executed later. If the document is ready the function is executed immediately. The problem isn't just with the ready function. If I let MVC handle everything, even a simple alert doesn't get executed. If I place the code below in the OnComplete handler, my code executes (including the ready function.)

        $("#editContent").html(context.get_data());
        return false;
    


    There is some difference in the way that MVC updates the DOM and the way the jQuery html function does that keeps javascript from executing. From the examples I've seen on the web what I did should have worked, although I don't remember seeing one that let MVC do all the work. Most of them used jQuery to either do all of the work or at least update the DOM.

  • Re: javascript not executing after ajax post

    06-26-2009, 12:26 PM
    Answer
    • Contributor
      5,773 point Contributor
    • Haacked
    • Member since 09-17-2003, 2:43 PM
    • Posts 388
    • AspNetTeam

    When you update the DOM with new HTML, the browser doesn't automatically execute scripts in the new bit of HTML. Our Ajax helpers would need to parse the partial HTML and try and execute the scripts, which is tricky and something we don't currently do.

    One approach you could take is to look at jQuery live events.

    Phil Haack (http://haacked.com/)
    Senior Program Manager, Microsoft

    What wouldn’t you do for a Klondike bar?
  • Re: javascript not executing after ajax post

    06-29-2009, 9:18 AM
    • Member
      point Member
    • JamesDalton
    • Member since 06-25-2009, 8:56 PM
    • Posts 5

    That explains the difference.  I have a couple of ideas on how to handle it, your jQuery live events suggestion being one of them, but I have another question about MVC, forms and posting that'll I'll post seperately.

  • Re: javascript not executing after ajax post

    08-12-2009, 7:40 AM
    • Member
      2 point Member
    • dotnethacker
    • Member since 07-30-2009, 6:18 PM
    • Posts 1

     Thanks, I have been having problems with this issue described here as well and will take a look at using JQuery to execute any scripts.

Page 1 of 1 (8 items)