Wait for the page to finish loading before executing
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Document</title>
<script type= "Text/javascript" >
Window.onload=function () {
Alert (Exist ("Getspantext"));
Alert (exist ("111"))
// }
function LoadText () {
var spanvalue = document.getElementById ("Getspantext"). InnerHTML;
Alert ("Spanvalue =" + Spanvalue);
Alert (Exist ("Getspantext"));
}
function exist (ID) {
var S=document.getelementbyid (ID);
if (s) {
return True
}
else{
return False
}
}
</script>
<body onload= "LoadText ()" >
<span id= "Getspantext" >jason</span "
</body>
Executes a piece of JavaScript immediately after the page is loaded:
onload="load()"
>
Try it yourself.
Browser support
IE |
Firefox |
Chrome |
Safari |
Opera |
|
|
|
|
|
The OnLoad property is supported by all major browsers.
Definition and usage
The OnLoad property is triggered when an object is loaded.
OnLoad is commonly used in <body>, and once all content is fully loaded (including images, script files, CSS files, etc.), a script is executed.
The difference between HTML 4.01 and HTML5
No.
Grammar
<element onload= "script" >
Property value
value |
Description |
Script |
The script that runs when onload occurs. |
JS OnLoad Event
Event Object
Definition and usage
The OnLoad event occurs immediately after the page or image has finished loading.
Grammar
Onload= "Somejavascriptcode"
Parameters |
Description |
Somejavascriptcode |
Necessary. Specifies the JavaScript that is executed when the event occurs. |
HTML tags that support the event:
<body>, <frame>, <frameset>, <iframe>, , <link>, <script>
JavaScript objects that support the event:
Image, layer, window
Instance
In this example, the text "page is loaded" will be displayed in the status bar:
onload="load()"
></body>
Tiy
-
OnLoad
-
How to use onload to display a piece of text in the status bar when the page finishes loading.
Event Object
Html/js OnLoad's explanation