Javascript – Call a code behind function from javascript in aspx

ccode-behindjavascript

I have a page where a javascript will be triggered when the user closes the browser tabs / browser window. Language is in c# thanks.

im using window.onbeforeunload, something along the line like this:

<script type="text/javascript">


    window.onbeforeunload = myFunction;


function myFunction()
{
    //call my function here
}

the code behind function will be a simple function for now, so no input parameters or return value are needed. So i'll just like to know how to call my function (eg: public void callMyFunction())

Best Answer

There are variety mechanisms to call your code-behind function(s). You could use an Ajax Call, Page Methods, ASP.NET Client Callbacks, or even trigger a code-behind handler using an invisible ASP.NET button.