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

Several ways to use OnLoad in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 17:46:17
Original
1867 people have browsed it

In the development of previous projects, I encountered several situations in which onload was used

1. It can be executed in the Body

2. First define and use

to copy the code after the page is loaded. The code is as follows:



Note: Here window.onload = test; Do not add () after test Because this is a method that is triggered by event association
If you add (), it will immediately trigger the execution of this method (this may not be our intention)
3. Use anonymous function
<script>window.onload = function(){alert(321);}</script>
4. How to write multiple onload events in JS?
<1> Just separate them with semicolons
<2>
window.attachEvent(" onload",function(){alert('a')});
window.attachEvent("onload",function(){alert('b')});
window.attachEvent("onload" ,function(){alert('c')});
Annotation: After using attachEvent to register the event, the registered event will be executed first
The above example will pop up C B A
One more thing :This method can only be used under IE (window.attachEvent)
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