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
...
...
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.
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"
Marked as answer by ricka6 on Aug 13, 2009 12:14 AM
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.)
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.
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?
Marked as answer by ricka6 on Jun 26, 2009 09:44 PM
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.
JamesDalton
0 Points
5 Posts
javascript not executing after ajax post
Jun 25, 2009 09:08 PM|LINK
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
...
...
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
lvzhq
Member
3 Points
7 Posts
Re: javascript not executing after ajax post
Jun 26, 2009 01:35 AM|LINK
Pls check your IIS version, if iis version is low some jquery function is invalid
JamesDalton
0 Points
5 Posts
Re: javascript not executing after ajax post
Jun 26, 2009 12:25 PM|LINK
This is happening running from VS2008. jQuery behaves as I would expect it's MVC that is acting strange.
paul.vencill
Contributor
6716 Points
1358 Posts
Re: javascript not executing after ajax post
Jun 26, 2009 02:02 PM|LINK
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.
JamesDalton
0 Points
5 Posts
Re: javascript not executing after ajax post
Jun 26, 2009 03:32 PM|LINK
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.
Haacked
Contributor
6901 Points
412 Posts
Re: javascript not executing after ajax post
Jun 26, 2009 04:26 PM|LINK
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.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
JamesDalton
0 Points
5 Posts
Re: javascript not executing after ajax post
Jun 29, 2009 01:18 PM|LINK
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.
DotNetHacker
Member
2 Points
1 Post
Re: javascript not executing after ajax post
Aug 12, 2009 11:40 AM|LINK
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.