So I'm loading a partialview using an ajax actionlink, but need to embed some jquery as part of the partialview that returns. What i'm strugling with is how to fire the script once its finished loading.
+2
A:
Use the callback of whichever jQuery function you are using (.post, .get, ajax).
For .get it looks like follows:
$.get( url, callbackFn );
function callbackFn(data){
//Append markup to dom
$('#someDiv').append(data)
// call the js function here
functionFromPartialView();
}
N.B I tend to bundle all js in separate files. There really is no need to embed js within partial views. Much easier and maintainable to keep a logical split.
redsquare
2009-07-23 11:00:09
Not using jquery to get the partialview? but need to run the jquery script that is part of the partialview. guess its not possible. Will try what you suggested.
Jan de Jager
2009-07-23 11:02:43
Yeah, ditch the ms ajax scripts. No real need for them as jQuery can do everything you need.
redsquare
2009-07-23 11:03:36
Got it going with the MS stuff. just had to hook up my function to the OnSuccess event... thanks for the tip anyway.
Jan de Jager
2009-07-23 11:11:06
no worries Jan, goodluck with it
redsquare
2009-07-23 11:20:17
+1
A:
Anyone know how to fire off some script in a PartialView without using the OnSuccess method?
I'm trying to implement an AJAX design where the controller & view can inject any sort of dialog box and behavior into a page, without the page needing to have specific knowledge of how to handle the dialog box.
So far hooking the OnSuccess callback is the only way that works, and this requires putting script on the page instead of the AJAX partial.
Any ideas guys?
Mark
2010-03-01 11:41:15
CAn you not maybe hook an event onto one of the objects that get initialized once the partial view is rendered? not exactly sure if this will work. I have had some luck with partials loading their own functions/script, but it is very intermittent and seems to only work in specific scenarios.
Jan de Jager
2010-03-02 08:58:43
Try adding a script block to the bottom of your partial view. Then add some code the a function() { .. }, and see if it fires..
Jan de Jager
2010-03-02 08:59:40