当前位置:网站首页>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
边栏推荐
- Appx代码签名指南
- Redis getting started complete tutorial: common exceptions on the client
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush
- leetcode
- 杰理之电话本获取【篇】
- How to verify accesstoken in oauth2 protocol
- Convert widerperson dataset to Yolo format
- Development of wireless communication technology, cv5200 long-distance WiFi module, UAV WiFi image transmission application
- Another million qubits! Israel optical quantum start-up company completed $15million financing
猜你喜欢
Construction of knowledge map of mall commodities
A complete tutorial for getting started with redis: problem location and optimization
Change your posture to do operation and maintenance! GOPs 2022 Shenzhen station highlights first!
Left value, right value
Dotconnect for DB2 Data Provider
又一百万量子比特!以色列光量子初创公司完成1500万美元融资
PSINS中19维组合导航模块sinsgps详解(时间同步部分)
杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
Cloud Mail . NET Edition
尚硅谷JVM-第一章 类加载子系统
随机推荐
【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
QT Bluetooth: qbluetooth DeviceInfo
uniapp的表单验证
Form validation of uniapp
A complete tutorial for getting started with redis: AOF persistence
应用程序启动速度的优化
Redis入门完整教程:问题定位与优化
QT common Concepts-1
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
杰理之播内置 flash 提示音控制播放暂停【篇】
知识图谱构建全流程
CVPR 2022 最佳论文候选 | PIP: 6个惯性传感器实现全身动捕和受力估计
杰理之开 BLE 退出蓝牙模式卡机问题【篇】
Convert widerperson dataset to Yolo format
美国空军研究实验室《探索深度学习系统的脆弱性和稳健性》2022年最新85页技术报告
The solution of unable to create servlet file after idea restart
Cryptography series: detailed explanation of online certificate status protocol OCSP
Laravel php artisan 自动生成Model+Migrate+Controller 命令大全
杰理之发射端在接收端关机之后假死机【篇】
Appx代码签名指南