c# - Run JavascriptJquery when partial loads - Stack Overflow

All the examplesanswers seem to show running a partial's javascript when the page loads.This part

All the examples/answers seem to show running a partial's javascript when the page loads.

This partial does not load when the page loads... It is loaded later on a button click using AJAX.

Ive tried my JS

inside:

$(document).ready(function () {

AND :

$(function(){

AND :

By itself

First doesnt trigger at all. Second and 3rd seem to trigger on second page load. I assume because the div that my js affects will exist. So how do I make it run when Partial loads..

All the examples/answers seem to show running a partial's javascript when the page loads.

This partial does not load when the page loads... It is loaded later on a button click using AJAX.

Ive tried my JS

inside:

$(document).ready(function () {

AND :

$(function(){

AND :

By itself

First doesnt trigger at all. Second and 3rd seem to trigger on second page load. I assume because the div that my js affects will exist. So how do I make it run when Partial loads..

Share Improve this question asked Jan 25, 2013 at 17:01 MclovingMcloving 1,4102 gold badges15 silver badges31 bronze badges 1
  • Can you post more code there is not enough here to determine your problem. $(function(){ should work in dynamic javascript as I am using this in several projects. I think something else is causing this – TheKingDave Commented Jan 25, 2013 at 17:05
Add a ment  | 

2 Answers 2

Reset to default 3
$(document).ready(function () {
    ...
});

Will trigger when the page is loaded (more concretely $(document).ready is wired to the document.DOMContentLoaded or window.loaded event) but because you load Partial with ajax, the page load event is already fired when the Partial is loaded.

If you use jQuery to load partial page via ajax I would suggest you to call your function from the

$ajax(...
}).done(function ( data ) {
    yourFunction();
});

Or if you use the AjaxHelper class (e.g. with razor @Ajax.ActionLink(... )) then use its AjaxOption's OnSuccess or OnComplete property to wire in your javascript function. Like the following way:

@Ajax.ActionLink("Load partial", "MyPartialAction", new AjaxOptions
{
    UpdateTargetId="partialContainer",
    OnSuccess="partialLoaded"
}

This code will load the Partial which is returned from the MyPartialAction action into the markup which has "partialContainer" id and after the Partial is loaded your javascript function named partialLoaded.

If you want to make it run when a partial loads, do it in the callback of whatever is issuing the request for the partial. If this request issuer is Jquery, it would be something like the following:

$.get(url, function (result) {
    // Do whatever you want here in the success callback.
});

If you are doing something with an MVC helper method such as Ajax.ActionLink, you can specify a jquery function to call upon response

@Ajax.ActionLink("NameOfLink", "ControllerAction", new AjaxOptions
{
    HttpMethod = "GET",
    OnSuccess = "javascriptFunction(data)"
})

function javascriptFunction(data) {
    // Do whatever you want
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745599205a4635300.html

相关推荐

  • c# - Run JavascriptJquery when partial loads - Stack Overflow

    All the examplesanswers seem to show running a partial's javascript when the page loads.This part

    12小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信