Home >Web Front-end >JS Tutorial >Automatically execute the js code of a method after the page is loaded_javascript skills

Automatically execute the js code of a method after the page is loaded_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:36:591535browse

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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn