Skip to content Skip to sidebar Skip to footer

How To Execute Javascript Function From Code-behind But Only After The Page Loads

I've found other solution on how to executes a javascript function from code-behind. The problem is that it firing before the page loads, so I can't add logic to it. if(result) {

Solution 1:

You might want to include the window.onload in your script file. something like this..

StringBuilder script = new StringBuilder();
script.Append("var existingHandler = window.onload;");
script.Append("window.document.body.onload = function(){ confirmPwdChange(); if (existingHandler){ existingHandler()}}");
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), 
                                           "Script", script.ToString(), true);

this ensure, your change password is called on page load and retains any other existing handlers declared already on the page.

Solution 2:

I think you're looking for PageRequestManager

List of PageRequestManager events

var prm = Sys.WebForms.PageRequestManager.getInstance();

// beginRequest: before postback to server
prm.add_beginRequest(BeginRequestHandler);

// endRequest: after postback to server
prm.add_endRequest(EndRequestHandler);

functionBeginRequestHandler(sender, args) {
    // do stuff
}

functionEndRequestHandler(sender, args) {
    // do stuff
}

Post a Comment for "How To Execute Javascript Function From Code-behind But Only After The Page Loads"