当前位置:网站首页>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
边栏推荐
- The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing
- 新标杆!智慧化社会治理
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
- [tools] basic concept of database and MySQL installation
- tensorboard的使用
- 2022年信息安全工程师考试大纲
- 凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
- Left path cloud recursion + dynamic planning
- 从零安装Redis
- Leetcode 77: combination
猜你喜欢

Left path cloud recursion + dynamic planning

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

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

尚硅谷JVM-第一章 类加载子系统
![[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication](/img/09/b9d50f7a10e6898ac4088ee005d00b.png)
[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication

tensorboard的使用

Utilisation de la promesse dans es6

Household appliance industry under the "retail is king": what is the industry consensus?

上个厕所的功夫,就把定时任务的三种调度策略说得明明白白

Software testing -- common assertions of JMeter interface testing
随机推荐
OC, OD, push-pull explanation of hardware
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
美国空军研究实验室《探索深度学习系统的脆弱性和稳健性》2022年最新85页技术报告
sshd[12282]: fatal: matching cipher is not supported: [email protected] [preauth]
How to find file accessed / created just feed minutes ago
The solution of unable to create servlet file after idea restart
Cryptography series: detailed explanation of online certificate status protocol OCSP
Unity uses maskablegraphic to draw a line with an arrow
从零安装Redis
uniapp适配问题
Kubernetes源码分析(二)----资源Resource
Error: could not find a version that satisfies the requirement xxxxx (from versions: none) solutions
【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
[tools] basic concept of database and MySQL installation
Redis入门完整教程:复制拓扑
Redis入门完整教程:问题定位与优化
Another million qubits! Israel optical quantum start-up company completed $15million financing
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
Development of wireless communication technology, cv5200 long-distance WiFi module, UAV WiFi image transmission application
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺