当前位置:网站首页>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
边栏推荐
- Hazel engine learning (V)
- 杰理之电话本获取【篇】
- Cryptography series: detailed explanation of online certificate status protocol OCSP
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
- Andrews - multimedia programming
- How to analyze fans' interests?
- 从控制理论的角度谈数据分析
- Introduction to ins/gps integrated navigation type
- Left value, right value
- 又一百万量子比特!以色列光量子初创公司完成1500万美元融资
猜你喜欢

从 1.5 开始搭建一个微服务框架——日志追踪 traceId

Unity uses maskablegraphic to draw a line with an arrow

Don't you know the relationship between JSP and servlet?

Es6中Promise的使用

Form validation of uniapp
MySQL is an optimization artifact to improve the efficiency of massive data query

杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】

硬件之OC、OD、推挽解释

LeetCode 77:组合

Use of promise in ES6
随机推荐
Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
Redis入门完整教程:问题定位与优化
Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】
Redis getting started complete tutorial: replication configuration
Metaforce force meta universe fossage 2.0 smart contract system development (source code deployment)
PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
Cloud Mail . NET Edition
HDU ACM 4578 Transformation-&gt;段树-间隔的变化
Shell 编程基础
新标杆!智慧化社会治理
MySQL is an optimization artifact to improve the efficiency of massive data query
Unity uses maskablegraphic to draw a line with an arrow
杰理之FM 模式单声道或立体声选择设置【篇】
oracle连接池长时间不使用连接失效问题
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺
密码学系列之:在线证书状态协议OCSP详解
Development of wireless communication technology, cv5200 long-distance WiFi module, UAV WiFi image transmission application
Left value, right value
How to analyze fans' interests?
应用程序启动速度的优化