简体   繁体   English

如何使用JavaScript在body onunload上调用多个函数?

[英]How to call multiple functions on body onunload using javascript?

I am trying to call two functions in body onunload event but didn't work. 我试图在body onunload事件中调用两个函数,但是没有用。

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. 每当我尝试仅调用updateStatus()函数时,它仍然不起作用。

its better to call a CombinedMethod which initiate the call to multiple methods. 最好调用CombinedMethod来启动对多个方法的调用。

<body onunload="CombinedMethods()">

and then inside method initiate the call to multiple methods.. 然后内部方法启动对多个方法的调用。

function CombinedMethods()
{
  closePopUps();
  updateStatus();
  M1();
  M2();
  etc_method();
}

You can call your functions in following way, 您可以通过以下方式调用函数,

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

If your second function is not working then check few things 如果您的第二个功能不起作用,请检查几件事

1.Set EnablePageMethods attribute to true 1.将EnablePageMethods属性设置为true
2. Mark your method as static and give it the WebMethod attribute (system.web.services;) Like this 2.将您的方法标记为静态,并为其赋予WebMethod属性(system.web.services;)。

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

Now try to acces pagemethod like this 现在尝试访问这样的pagemethod

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. 并尝试将这两种方法结合起来并在一个方法中调用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM