当前位置:网站首页>Domcontentloaded and window onload
Domcontentloaded and window onload
2022-07-07 03:10:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
Believe in writing js Of . We all know window.onload Well , But not everyone knows DOMContentLoaded, In fact, even if you don't know . It's very likely that you often use this thing .
In general ,DOMContentLoaded The event should be in window.onload Run before , When DOM When the tree is built, it will run DOMContentLoaded event , and window.onload When the page is loaded , To run . This includes elements such as pictures . Most of the time we just want to DOM After the tree is built , Binding events to elements . We don't need image elements . Plus sometimes the speed of loading Outland pictures is very slow .
We can write code to simply test these two events :
javascript Code ( Introduced jQuery1.4.1):
if (document.addEventListener){ function DOMContentLoaded(){ $( "#status" ).text( "DOM is ready now!" ); } document.addEventListener( "DOMContentLoaded" , DOMContentLoaded, false ); } window.onload= function (){ $( "#status" ).text( "DOM is ready AND wondow.onload is excute!" ); } |
|---|
HTML Code body part :
<h1> DOM READY's TEST </h1>
<img src="./delay.php" alt="delay image" />
<p id="status"> DOM is not ready </p>In order to see the effect clearly , Specially write a simple php file , Provide delayed loading of pictures , The code is as follows :
sleep(5);
header('Location:./delay.png');stay firefox and chrome as well as opera Can clearly see , Before the picture is loaded .id by status The paragraph of has been shown “DOM is ready now!”. Then wait 5 Seconds later , After the picture is loaded , This paragraph shows ”DOM is ready AND wondow.onload is excute!”
This code cannot be used in IE Work in China , On the one hand, I use addEventListener, I made an inference before I started , If this method does not exist, this event will not be added . And the reason for that is IE It really doesn't support DOMContentLoaded The cause of this incident is , For the simplicity of the code . Not for IE Yes . Even though IE There is no such event , But we can simulate this event , The common method is to infer element Of doScroll Assuming success, it means DOM Loading finished .
Common libraries provide encapsulation of this event compatible with various browsers . Suppose you are a jQuery Users , You may often use (document).ready(); Or by (function(){}) This is for DOMContentLoaded Major events
Copyright notice : This article is the original article of the blogger . Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116789.html Link to the original text :https://javaforall.cn
边栏推荐
- 尚硅谷JVM-第一章 类加载子系统
- How-PIL-to-Tensor
- Redis入门完整教程:客户端案例分析
- SQL中删除数据
- 应用程序启动速度的优化
- Leetcode 77: combination
- Redis入門完整教程:問題定比特與優化
- Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform
- PSINS中19维组合导航模块sinsgps详解(滤波部分)
- 2022 information security engineer examination outline
猜你喜欢
![[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]](/img/f0/79e7915ba3ef32aa21c4a1d5f486bd.jpg)
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]

LeetCode 77:组合

【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程

Cryptography series: detailed explanation of online certificate status protocol OCSP

How to analyze fans' interests?

Es6中Promise的使用

又一百万量子比特!以色列光量子初创公司完成1500万美元融资

How-PIL-to-Tensor

Construction of knowledge map of mall commodities

Use of tensorboard
随机推荐
How to write test cases for test coupons?
Redis入门完整教程:客户端常见异常
Examples of how to use dates in Oracle
Introduction to ins/gps integrated navigation type
Change your posture to do operation and maintenance! GOPs 2022 Shenzhen station highlights first!
IDEA重启后无法创建Servlet文件的解决方案
Development of wireless communication technology, cv5200 long-distance WiFi module, UAV WiFi image transmission application
[tools] basic concept of database and MySQL installation
How does C language (string) delete a specified character in a string?
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
Redis入门完整教程:客户端案例分析
INS/GPS组合导航类型简介
Andrews - multimedia programming
Redis入门完整教程:问题定位与优化
A complete tutorial for getting started with redis: RDB persistence
Redis入门完整教程:RDB持久化
杰理之RTC 时钟开发【篇】
你知道电子招标最突出的5大好处有哪些吗?
The version control of 2021 version is missing. Handling method