GlobalEventHandlers.onload - Web APIs 编辑
The onload
property of the GlobalEventHandlers
mixin is an EventHandler
that processes load
events on a Window
, XMLHttpRequest
, <img>
element, etc.
The load
event fires when a given resource has loaded.
Syntax
target.onload = functionRef;
Value
functionRef
is the handler function to be called when the window’s load
event fires.
Examples
window.onload = function() {
init();
doSomethingElse();
};
<!doctype html>
<html>
<head>
<title>onload test</title>
// ES5
<script>
function load() {
console.log("load event detected!");
}
window.onload = load;
</script>
// ES2015
<script>
const load = () => {
console.log("load event detected!");
}
window.onload = load;
</script>
</head>
<body>
<p>The load event fires when the document has finished loading!</p>
</body>
</html>
Notes
The load
event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.
There are also DOM Events like DOMContentLoaded
and DOMFrameContentLoaded
(which can be handled using EventTarget.addEventListener()
) which are fired after the DOM for the page has been constructed, but do not wait for other resources to finish loading.
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'onload' in that specification. | Living Standard | Initial definition |
Browser compatibility
BCD tables only load in the browser
See also
load
eventDOMContentLoaded
event in Listening to events: Simple DOM events- IIFE Immediately-invoked function expression
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论