run more than one javascript function on page load - Stack Overflow

I want run some javascript functions on page load, which ways are exist and which one is better ?can i

  1. I want run some javascript functions on page load, which ways are exist and which one is better ?
  2. can i call more than one function in <body onclick="my function();"> ?
  1. I want run some javascript functions on page load, which ways are exist and which one is better ?
  2. can i call more than one function in <body onclick="my function();"> ?
Share Improve this question asked May 18, 2011 at 13:56 biglibiglibiglibigli 591 gold badge3 silver badges8 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

First, you do not want to force the user to click on the page in order for the functions to execute.

The most basic way to do is:

<body onload="yourFunction();">

Then, in a <script> tag in the <head> have your function call all your other functions:

function yourFunction(){
    function1();
    function2();
    // some other code not in a function...
    //...
}
<body onload="yourFunction1();yourFunction2();">

If you want to add different listeners, you'll need to use javascript. Check the answer to this question (edit: ok "need" is a bit strong, you can still do it directly in the tag :))

Executing code on page load is not a trivial task because of cross-browser patibility. It is better to use one of the frameworks. For example, jQuery:

$(function () { /* your code */ });

As for binding several event functions to element - you can do it with jquery too:

$('#mybytton').click(function () { /* first handler */ });
$('#mybytton').click(function () { /* second handler */ });

or

$('#mybytton').click(function () { /* first handler */ })
    .click(function () { /* second handler */ });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744728767a4590338.html

相关推荐

  • run more than one javascript function on page load - Stack Overflow

    I want run some javascript functions on page load, which ways are exist and which one is better ?can i

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信