Home >Web Front-end >JS Tutorial >Automatically execute the js code of a method after the page is loaded_javascript skills
1. Use onload:
in body
<body onload="myfunction()">
2. Use window.onload:
in the script
<script type="text/javascript"> function myfun() { alert("this window.onload"); } /*用window.onload调用myfun()*/ window.onload=myfun;//不要括号 </script>
The following example changes the background color of id_1 after the page has finished loading.
<script language="javascript" > function myfun() { document.getElementById("id_1").style.background= "#000"; // 改变背景颜色 } // 用js实现在加载完成一个页面后自动执行一个方法 /*用window.onload调用myfun()*/ window.onload=myfun;//不要括号 </script>