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
Release: 2016-05-16 16:36:59
Original
1571 people have browsed it

1. Use onload:
in body

<body onload="myfunction()">
Copy after login

2. Use window.onload:
in the script

<script type="text/javascript">
function myfun()
{
alert("this window.onload");
}
/*用window.onload调用myfun()*/
window.onload=myfun;//不要括号
</script>
Copy after login

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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template