0

I am trying to call two functions in body onunload event but didn't work.

Below is the first function that I am calling..

function closePopUps() 
{
    if(childPops.length == 0) return; 

      for(i=0; i<childPops.length; i++) 
        {
           childPops[i].close(); 
        }
}

Then the second function...

function updateStatus()
{
 PageMethods.UPDATE_STATUS();
}

I called them like this...

<body onunload="closePopUps();updateStatus();">

But the second function doesn't work. It still didn't work whenever I tried to call only the updateStatus() function.

3 Answers 3

2

its better to call a CombinedMethod which initiate the call to multiple methods.

<body onunload="CombinedMethods()">

and then inside method initiate the call to multiple methods..

function CombinedMethods()
{
  closePopUps();
  updateStatus();
  M1();
  M2();
  etc_method();
}
Sign up to request clarification or add additional context in comments.

1 Comment

but I think it should work.. Well chrome doesn't support onunload, else I must have tried it.. you can see http://jsfiddle.net/uk85zzhh/ try it in IE its working.. hit the url then refresh the page or open the new url in same tab.. it will show three alert M1, M2 and M3
0

If your second function is not working then check few things

1.Set EnablePageMethods attribute to true
2. Mark your method as static and give it the WebMethod attribute (system.web.services;) Like this

[WebMethod]
    public static string sample(string name)
    {
        return  name;
    }

Now try to acces pagemethod like this

function updateStatus()
{
 PageMethods.sample("Test");
}

Now your second function will also work. And try to combine both the method and call them in a single method.

7 Comments

I've already set EnablePageMethods of ScriptManager to true. Then i tried to call the function in a button click and it works. But when I call it in onunload event it didn't
have you tried "onbeforeunload" event.You should call your second method on "onbeforeunload" and first one "Onunload" event ..Try this
It could also problem of browser. Some time the newer version of firefox do not respond to pagemethod request.Try your code in different browser Or it could be due to your first method try to call it before your first method
Yes i've already tried calling that in onbeforeunload event and it works, but the problem is it didn't return a confirmation message...
hey can you try this window.onbeforeunload = updateStatus; //(* without '()')
|
0

You can call your functions in following way,

$(document).ready(function(){
    window.onunload = function(evt) {
       //call your functions
    };
});

4 Comments

What is evt? Is that a javascript word?
it's an event object.
Yup, write above code in your javascript onReady function. Check edited answer.
have you tried with window.onbeforeunload ? also I made a small typo mistake in my answer please refer edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.