views:

454

answers:

2

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
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
Yeah, ditch the ms ajax scripts. No real need for them as jQuery can do everything you need.
redsquare
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
no worries Jan, goodluck with it
redsquare
+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
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
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