Home > Web Front-end > JS Tutorial > body text

Standard use of window.onload code example in js (including code)

亚连
Release: 2018-05-18 11:18:30
Original
1757 people have browsed it

The following is a code example that I compiled for you to standardize the use of window.onload in js. Interested students can take a look.

1. Code that can be executed normally by window.onload

<html>
<head>
<script>
window.onload=function()
{
alert(1);
}
</script>
</head>
<body>
<div id="div">sdfasdf</div>
</body>
</html>
Copy after login

2. Code that cannot be executed normally

<html>
<head>
<script>
window.onload=function()
{
alert(1);
}
function()
{
alert("z");
}
</script>
</head>
<body onload="z()">
<div id="div">sdfasdf</div>
</body>
</html>
Copy after login

3. So where is the problem?

You can see that there is no js code except window.onload in the first piece of code, and there is an extra function z() in the second piece of code, and the second This piece of code specifies that the onload of the body is z(), so when the second piece of code is executed, it will only execute the js code in the z() function, and will not execute the js code in window.onload. Therefore, when you specify onload yourself, window.onload is invalid.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Explain to you in detail how to create div, span, label in js through code

How to create in js model data model (code provided, simple and easy to understand)

Native JS implementation method to determine collision (interesting example)

The above is the detailed content of Standard use of window.onload code example in js (including code). For more information, please follow other related articles on the PHP Chinese website!

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