当前位置:网站首页>DOMContentLoaded和window.onload
DOMContentLoaded和window.onload
2022-07-06 19:37:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
相信写js的。都知道window.onload吧,可是并非每一个人都知道DOMContentLoaded,事实上即使你不知道。非常有可能你也常常使用了这个东西。
普通情况下,DOMContentLoaded事件要在window.onload之前运行,当DOM树构建完毕的时候就会运行DOMContentLoaded事件,而window.onload是在页面载入完毕的时候,才运行。这当中包含图片等元素。大多数时候我们仅仅是想在DOM树构建完毕后,绑定事件到元素。我们并不须要图片元素。加上有时候载入外域图片的速度很缓慢。
我们能够写代码来简单測试一下这两种事件:
javascript代码(引入了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 代码 body 部分:
<h1> DOM READY's TEST </h1>
<img src="./delay.php" alt="delay image" />
<p id="status"> DOM is not ready </p>
为了清楚的看到效果,特意写个简单的php文件,提供图片延时载入,代码例如以下:
sleep(5);
header('Location:./delay.png');
在firefox和chrome以及opera中都能够清楚的看到,在图片未载入之前。id为status的段落已经显示了“DOM is ready now!”。然后等5秒钟后,图片载入完毕后,此段落显示”DOM is ready AND wondow.onload is excute!”
此代码并不能在IE中工作,一方面是由于我使用的是addEventListener,開始前做了个推断,不存在此方法则不加入此事件。这样做的原因是IE确实是不支持DOMContentLoaded这个事件的,为了代码的简单。就没为IE写了。尽管IE没有此事件,可是我们却能够来模拟这个事件,常见的方法是推断element的doScroll假设成功则说明DOM加载完毕。
常见的库中都提供了此事件的兼容各个浏览器的封装。假设你是个jQuery使用者,你可能会常常使用(document).ready();或靠(function(){}) 这是用来DOMContentLoaded大事
版权声明:本文博主原创文章。博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116789.html原文链接:https://javaforall.cn
边栏推荐
- Redis入门完整教程:AOF持久化
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (initial assignment part)
- A complete tutorial for getting started with redis: AOF persistence
- Software testing -- common assertions of JMeter interface testing
- Redis introduction complete tutorial: replication principle
- Planning and design of double click hot standby layer 2 network based on ENSP firewall
- ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法
- MySQL提升大量数据查询效率的优化神器
- Construction of knowledge map of mall commodities
- Redis入门完整教程:复制原理
猜你喜欢
IDEA重启后无法创建Servlet文件的解决方案
【2022国赛模拟】多边形——计算几何、二分答案、倍增
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
Es6中Promise的使用
[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication
Redis入门完整教程:复制拓扑
Change your posture to do operation and maintenance! GOPs 2022 Shenzhen station highlights first!
MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
Summary of basic debugging steps of S120 driver
The 8 element positioning methods of selenium that you have to know are simple and practical
随机推荐
Classify the features of pictures with full connection +softmax
左程云 递归+动态规划
tensorboard的使用
sshd[12282]: fatal: matching cipher is not supported: [email protected] [preauth]
Redis入门完整教程:问题定位与优化
Planning and design of double click hot standby layer 2 network based on ENSP firewall
Redis入门完整教程:客户端案例分析
杰理之发射端在接收端关机之后假死机【篇】
[socket] ① overview of socket technology
Django database (SQLite) basic introductory tutorial
How to analyze fans' interests?
C language exercises_ one
Statistics of radar data in nuscenes data set
A complete tutorial for getting started with redis: AOF persistence
Starting from 1.5, build a micro Service Framework -- log tracking traceid
The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
Centerx: open centernet in the way of socialism with Chinese characteristics
Install redis from zero
wzoi 1~200