有没有网页打开时,就执行的JS函数

JavaScript07

有没有网页打开时,就执行的JS函数,第1张

将JS代码,插入到head区域,即可在网页打开时执行。

示例:

<html>

<head><title>test</title></head>

<script>

alert("已经开始执行函数!")

</script>

<body></body>

</html>

<html>

    <body>

        <button id="button" onclick="sendMsg()">自动执行</button>

    </body>

</html>

<script type="text/javascript">

    window.onload = function(){

        var button = document.getElementById('button')

        button.click()//执行执行点击按钮

    }

    var sendMsg = function(){

      alert('按钮已点击执行')  

    }

</script>