zl程序教程

您现在的位置是:首页 >  其它

当前栏目

How to run a function when the page is loaded?

to The is How Function when run Page
2023-09-11 14:14:20 时间

How to run a function when the page is loaded?

window.onload = codeAddress; should work - here's a demo, and the full code:

方法1

<!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>

 

方法2

<!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');
        }
        
        </script>
    </head>
    <body onload="codeAddress();">
    
    </body>
</html>