JavaScript 函数调用

发布于 2024-09-30 16:33:53 字数 170 浏览 4 评论 0原文

如何在窗口的onload事件上调用多个javascript函数?

例如,

             window.onload=MyFunc(); //Single Function

但是如果在窗口的 onload 事件上调用多个函数怎么办?

How to call more than one javascript function on the window's onload event?

For Ex,

             window.onload=MyFunc(); //Single Function

But what if there are more than one functions to call on the window's onload event...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莳間冲淡了誓言ζ 2024-10-07 16:33:53

把它们包起来。

window.onload = function() { MyFunc(); MyOtherFunc(); }

Wrap them.

window.onload = function() { MyFunc(); MyOtherFunc(); }
混浊又暗下来 2024-10-07 16:33:53

或者您可以将函数绑定到窗口的加载事件:

window.addEventListener("load", MyFunction, false);
window.addEventListener("load", MyOtherFunction, false);

对于 IE 旧版本,您可能需要使用 AttachEvent 方法:

window.attachEvent("load", MyFunction);
window.attachEvent("load", MyOtherFunction);

这种方法的问题是函数执行的顺序无法确定。

Or you could bind the functions to the window's load event:

window.addEventListener("load", MyFunction, false);
window.addEventListener("load", MyOtherFunction, false);

For IE legacy you might need to use the attachEvent method:

window.attachEvent("load", MyFunction);
window.attachEvent("load", MyOtherFunction);

Problem with this approach is that the order in which the functions are executed can not be counted on.

凑诗 2024-10-07 16:33:53

这是一个解释如何操作的示例:

// create an array that will contain all 
// your functions that you would like to 
// call on page load
var pageOnLoadEvents = [];

// Add one or more functions in pageOnLoadEvents array
pageOnLoadEvents.push(function() { alert("Hello!"); })
pageOnLoadEvents.push(function() { alert("How are you?"); })


// This will call when your page will load
window.onload = function() {
  // Loop through each index of pageOnLoadEvents
  for(var index = 0; index < pageOnLoadEvents.length; index++) {
   // '()' brackets in the end tell them to make a call
   // If you don't include '()' at the end this will
   // suspect as a object as in JavaScript functions 
   // are first-class objects.
   // document.write(pageOnLoadEvents[index].toString()); // treat as object
   pageOnLoadEvents[index]();
  }
}

上面的示例试图变得简单,以便为您的问题提供解释。不过,Simon Willison 的博客上有一个很好的解释,他写道:

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  /* more code to run on page load */ 
});

addLoadEvent 函数作为
参数另一个函数应该
页面加载后执行。
与直接赋值不同
window.onload,该函数添加了
事件以任何方式
之前添加的 onload 函数将
首先被执行。 了解更多

Here is a sample piece that would explain HOW:

// create an array that will contain all 
// your functions that you would like to 
// call on page load
var pageOnLoadEvents = [];

// Add one or more functions in pageOnLoadEvents array
pageOnLoadEvents.push(function() { alert("Hello!"); })
pageOnLoadEvents.push(function() { alert("How are you?"); })


// This will call when your page will load
window.onload = function() {
  // Loop through each index of pageOnLoadEvents
  for(var index = 0; index < pageOnLoadEvents.length; index++) {
   // '()' brackets in the end tell them to make a call
   // If you don't include '()' at the end this will
   // suspect as a object as in JavaScript functions 
   // are first-class objects.
   // document.write(pageOnLoadEvents[index].toString()); // treat as object
   pageOnLoadEvents[index]();
  }
}

The above sample tried to be made simple to give you explanation on your question. However, there is a good explanation on Simon Willison’s Weblog, he wrote:

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  /* more code to run on page load */ 
});

The addLoadEvent function takes as an
argument another function which should
be executed once the page has loaded.
Unlike assigning directly to
window.onload, the function adds the
event in such a way that any
previously added onload functions will
be executed first. Read more.

兲鉂ぱ嘚淚 2024-10-07 16:33:53

为什么不采用一种老式的方式在页面加载时调用一个函数内尽可能多的函数调用?
window.addEventListener("加载", Foo, false);

其中 Foo 调用了所有必需的函数。
这样您就可以根据需要控制顺序和返回值。

Why not have an old school way of having as many function calls inside one function invoked on page load?
window.addEventListener("load", Foo, false);

where Foo has calls to all required functions.
This way you can control the order and return values if required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文