当前位置:网站首页>Three methods to realize JS asynchronous loading
Three methods to realize JS asynchronous loading
2022-07-07 00:35:00 【Cut potatoes into shreds】
One 、 Why write Load asynchronously :
①js Loading itself belongs to Synchronous loading Of , load js Document meeting Blocking documents , Once the network speed is bad , So the whole site will wait js Loading without subsequent rendering, etc . But some tools and methods need Load on demand , There are some tools js File it will not change the page , Use reload , No need not load .
② But in the actual development, we only need to put script The tag is placed at the bottom of the page . There is no need to write Redundant code , But loading on demand must be the top priority of the interview !!!
Two 、JS Load asynchronously There are three solutions to this problem :
①defer
script Add... To the label defer attribute , Load asynchronously
1. But wait dom All documents have been parsed (dom Tree generated ) To be executed .
2. Only IE It works ;
②async
script Add... To the label async attribute , Load asynchronously
1. Execute after loading ;async Only external scripts can be loaded
2. Can't take js Written in script In the label .
3.w3c standard ,IE9 The following is not supported
③ Encapsulate a function compatible asynchronous load js File, and the functions in the file can be executed on demand ( Load on demand )
<script>
function loadScript(url,callback){
//url It's loaded on demand js file
//callback It's loaded on demand js A function in the file
// 1. Create a script label
var script = document.createElement('script');
// Handle ie Compatibility
if(script.readyState){
script.onreadystatechange = function(){
// If script Download completed
if(script.readyState == 'complete' || script.readyState == 'loaded'){
callback();
}
}
}else{
// monitor script Download status of When the status changes to download complete Re execution callback
script.onload = function(){
callback();
}
}
// The purpose introduced later is if IE If the download is too fast ( Faster than reading programs )
//IE Of readystatechange When the event detects the status code , It's already from loading become complete perhaps loaded( Finished loading the file at a very fast speed , You haven't had time to test )
// Then if you test it again, it won't change , It's always been complete perhaps loaded
// This time is a hindsight , The test is useless .
// 2. to script add src The introduction of a js file
script.src = url;
// 3. Append to body
document.body.appendChild(script);
}
</script>
3、 ... and 、 Finally added : document Three states
①loading Loading status ,dom The tree is being drawn
②interactive Active state ,dom Tree drawing complete
③complete Completion status ( Older browsers may be loaded),dom The tree drawing is completed and all resources are downloaded
This is recorded for future use , I also hope the big guys can communicate more , Leave more messages , Point out my shortcomings !
You can study it if you need it !!
边栏推荐
- How to judge whether an element in an array contains all attribute values of an object
- Value Function Approximation
- "Latex" Introduction to latex mathematical formula "suggestions collection"
- Things like random
- How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
- What is a responsive object? How to create a responsive object?
- 2022/2/11 summary
- 准备好在CI/CD中自动化持续部署了吗?
- Business process testing based on functional testing
- Leecode brush questions record sword finger offer 11 Rotate the minimum number of the array
猜你喜欢
Business process testing based on functional testing
ldap创建公司组织、人员
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
【软件逆向-自动化】逆向工具大全
How to set encoding in idea
uniapp中redirectTo和navigateTo的区别
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
【软件逆向-求解flag】内存获取、逆变换操作、线性变换、约束求解
基于SSM框架的文章管理系统
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
随机推荐
【软件逆向-自动化】逆向工具大全
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
How to set encoding in idea
Personal digestion of DDD
Quickly use various versions of PostgreSQL database in docker
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
TypeScript增量编译
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
GPIO簡介
《LaTex》LaTex数学公式简介「建议收藏」
js导入excel&导出excel
Sword finger offer 26 Substructure of tree
Encryption algorithm - password security
【CVPR 2022】半监督目标检测:Dense Learning based Semi-Supervised Object Detection
Command line kills window process
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
Google, Baidu and Yahoo are general search engines developed by Chinese companies_ Baidu search engine URL
基于GO语言实现的X.509证书
The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation
Cross-entrpy Method