Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top