How do I call a JavaScript function on page load?

后端 未结 8 1345
轮回少年
轮回少年 2020-11-22 15:18

Traditionally, to call a JavaScript function once the page has loaded, you\'d add an onload attribute to the body containing a bit of JavaScript (usually only c

相关标签:
8条回答
  • 2020-11-22 15:28

    You have to call the function you want to be called on load (i.e., load of the document/page). For example, the function you want to load when document or page load is called "yourFunction". This can be done by calling the function on load event of the document. Please see the code below for more detail.

    Try the code below:

    <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
        $(document).ready(function () {
            yourFunction();
        });
        function yourFunction(){
          //some code
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 15:31
    function yourfunction() { /* do stuff on page load */ }
    
    window.onload = yourfunction;
    

    Or with jQuery if you want:

    $(function(){
       yourfunction();
    });
    

    If you want to call more than one function on page load, take a look at this article for more information:

    • Using Multiple JavaScript Onload Functions
    0 讨论(0)
  • 2020-11-22 15:35

    Another way to do this is by using event listeners, here how you use them:

    document.addEventListener("DOMContentLoaded", function() {
      you_function(...);
    });
    

    Explanation:

    DOMContentLoaded It means when the DOM Objects of the document are fully loaded and seen by JavaScript, also this could have been "click", "focus"...

    function() Anonymous function, will be invoked when the event occurs.

    0 讨论(0)
  • 2020-11-22 15:45

    here's the trick (works everywhere):

    r(function(){
    alert('DOM Ready!');
    });
    function r(f){/in/.test(document.readyState)?setTimeout('r('+f+')',9):f()}
    
    0 讨论(0)
  • 2020-11-22 15:46

    Your original question was unclear, assuming Kevin's edit/interpretation is correct then this first option doesn't apply

    The typical options is using the onload event:

    <body onload="javascript:SomeFunction()">
    ....
    

    You can also place your javascript at the very end of the body; it won't start executing until the doc is complete.

    <body>
      ...
      <script type="text/javascript">
        SomeFunction();
      </script>
    </body>
    

    And, another options, is to consider using a JS framework which intrinsically does this:

    // jQuery
    $(document).ready( function () {
      SomeFunction();
    });
    
    0 讨论(0)
  • 2020-11-22 15:49

    <!DOCTYPE html>
    <html>
        <head>
            <title>Test</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <script type="text/javascript">
            function codeAddress() {
                alert('ok');
            }
            window.onload = codeAddress;
            </script>
        </head>
        <body>
        
        </body>
    </html>

    0 讨论(0)
提交回复
热议问题