当前位置:网站首页>How to delay loading JS
How to delay loading JS
2022-07-19 11:59:00 【French speaking pig】
JS Delayed loading is waiting dom( page ) Load after loading js file , This helps speed up page loading , Better user experience
Generally delayed loading js There are ways :
① defer attribute
② async attribute
③ Dynamically create DOM
④ Use jq Of getScript Method
⑤ Use setTimeout Delay loading scripts
⑥ take script Import last ( Bottom ) load
One of the more important things is defer and async The way
Here's a chestnut :
a.js
console.log(document.getElementById('myDiv'), '----->>>')test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- External introduction a.js -->
<script src="./a.js"></script>
</head>
<body>
<div id="myDiv">wft</div>
</body>
</html>In this way , We can't get it div Elements , Print out null , This is because it is implemented first script( namely a.js) The code in , Then load the following dom Elements , Naturally, I can't get it . At this time, if you say a.js An error occurred in , It will cause nothing to be loaded on the page .
If we say we will script Put it at the end of the execution , as follows :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="myDiv">wft</div>
<script src="./a.js"></script>
</body>
</html>At this time, it will print out normally div Elements , This is what we said above, put it at the bottom and finally execute
Put it on top and add defer, as follows :
defer
<!-- External introduction a.js -->
<script defer src="./a.js"></script>We found that it can print normally div Elements
although
<script>Elements are placed<head>In the elements , But the included script will delay the browser from encountering</html>Label and then executeJust wait html All analysis completed , Will execute js Code , In turn js Script
async
Similarly, we will the above defer Switch to async, as follows :
<!-- External introduction a.js -->
<script async src="./a.js"></script>We found that we can get the same div Elements , It also implements the delayed loading script
HTML5 by
<script>The label definesasyncattribute . AnddeferProperties are similar to , Are used to change the behavior of processing scripts . Again , Only for external script files .
Purpose : Don't let the page wait for the script to download and execute , thus Load other contents of the page asynchronously .Asynchronous scripts must be on the page load Before the event .
There's no guarantee that scripts will execute in sequence .
defer and async difference
defer It is executed in sequence , but async No .
async You cannot control the loading order of external scripts
such as :
<!-- External introduction a.js -->
<script defer src="./a.js"></script>
<script defer src="./b.js"></script>
<script defer src="./c.js"></script>The above delays the loading of three scripts , Now we use defer, When loading scripts , It must be according to a - > b -> c The order of ( That is to execute first a.js Re execution b.js Finally, execute c.js) Executive , If replaced async, You can also delay loading js Script , But the order is uncertain , Whoever loads first will execute first , So if you say c.js Depend on b.js, Then you must not use async.
边栏推荐
- Redis分布式缓存-Redis集群
- [binomial tree] the power of the button cattle customers must brush questions
- Nintendo patent shows that the follow-up products of fitness ring accessories may be under development
- Tiflash performance tuning
- TiFlash 性能调优
- Project construction depends on people, and success depends on people!
- LeetCode_17_电话号码的字母组合
- Genesis与BlueRun Ventures展开深度交流
- The basic establishment of the sequence table and the related operations of adding, deleting, modifying and querying (the sequence table described in C language)
- Leetcode 1304. N different integers with zero and
猜你喜欢

C # build a system based on WPF entry project of net5

Detailed explanation of MySQL show processlist

HCIP (7)

Configure spectrum navigation for Huawei wireless devices

传输层 -------- TCP(一)

02-3、指针和引用的区别

Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning

NAT technology and NAT alg

【二叉树】之力扣牛客必刷题

Learning outline of the column "MySQL DBA's magic road"
随机推荐
Push down calculation result cache
function/symbol ‘pango_ context_ set_ round_ glyph_ positions‘ not found in library ‘libpango-1.0. so. 0‘x
Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
聚焦绿色发展新赛道—MapGIS智慧环保解决方案发布
Redis Distributed cache - Redis Cluster
LeetCode_77_组合
565. Array nesting: regular simulation questions
TiKV Follower Read
QT learning diary 17 - QT database
Why does the magnetic variable speed gear reverse?
Lychee sound quality high fidelity AI noise reduction technology sharing
Project construction depends on people, and success depends on people!
[vulnhub range] prime:1 record of shooting process
Solution of connecting MySQL instance with public network
03-2、
SQL union operator
Leetcode 1252. 奇数值单元格的数目
Wi Fi sensing technology and practice based on channel state information
High performance IO framework library libevent (III): overview of libevent framework functions
【嵌入式单元测试】C语言单元测试框架搭建