


Specific methods of non-blocking loading in Javascript_javascript tips
Read the reading notes of "High Performance JavaScript"
A few principles:
1. Place the script at the bottom
is still in the head to ensure that the page can be loaded normally before js is loaded.
<script> is placed before </body>. </p> <p><strong>2. Group script</strong></p> <p> Since each <script> tag blocks the page parsing process when downloaded, limiting the total number of <script>s on a page can also improve performance. Applies to both inline and external scripts. </p> <p><strong>3. Non-blocking script</strong></p> <p>Wait until the page finishes loading, then load the js code. That is, the code download starts after the window.load event is emitted. </p> <p> (1) defer attribute: supports IE4 and fierfox3.5 and higher browsers </p> <p><script defer>...</script>
Inline and external files
<script> with defer attribute can appear anywhere in the document. The corresponding js file will start downloading when <script> is parsed, but the code will not be executed until the DOM is loaded (in the onload event handler before being called). Therefore, parallel downloading is implemented and also shows off other resources. </p>
<p> (2) Dynamic script elements </p>
<p>The Document Object Model (DOM) allows you to dynamically create almost all document content of HTML using js. <br></p>
<div class="codetitle">
<span><a style="CURSOR: pointer" data="81426" class="copybut" id="copybut81426" onclick="doCopy('code81426')"><u>Copy code</u></a></span> The code is as follows:</div>
<div class="codebody" id="code81426">
<br>var script=document.createElement("script");
<p>script.type="text/javascript";</p>
<p>script.src="file.js";</p>
<p>document.getElementByTagName_r("head")[0].appendChild(script);<br></p>
</div>
<br>The key point of this technology is: no matter where the download is initiated, the file will not download and run Block other page processing. Even in the head (except the http link for downloading the file).
<p>(3)The YUI3 approach</p>
<p> Concept: Use a small initial code, download the rest of the functional code, introduce the file first: <br></p>
<div class="codetitle">
<span><a style="CURSOR: pointer" data="73140" class="copybut" id="copybut73140" onclick="doCopy('code73140')"><u>Copy code</u></a></span> Code As follows:</div>
<div class="codebody" id="code73140">
<br><script type="text/javascript src=http://files.jb51.net/file_images/article/201306/yuanma/combo.js></script>
This torrent file is about 10KB,
Use:
YUI().use(" dom",function(Y){
Y.Dom.addclass(...)
})
When all the code is available, the callback function is called, the YUI instance is passed in as a parameter, and the newly downloaded functionality can be used immediately.
The LazyLoad library
Usage: First introduce: lazyload-min.js
(4)
LazyLoad.js("a .js",function(){
Application.init();
})
Multiple files:
LazyLoad.js(["a.js","b.js"],function(){
Application.init();
})
(5)The LABjs library
First introduce: lab.js
$LAB. script("a.js").wait(function(){
Application.init();
})
For multiple files, write them in a chain
His unique feature is the ability to manage dependencies.
You can specify which files should wait for other files through the wait() function.
For example: b.js code is guaranteed not to run before a.js
$LAB.script("a.js").wait().script("b.js").wait(function(){
Application.init();
})
In this way, although the two files are downloaded in parallel, it can ensure that a.js can be executed before b.js

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics


![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Subtitles not working on Stremio on your Windows PC? Some Stremio users reported that subtitles were not displayed in the videos. Many users reported encountering an error message that said "Error loading subtitles." Here is the full error message that appears with this error: An error occurred while loading subtitles Failed to load subtitles: This could be a problem with the plugin you are using or your network. As the error message says, it could be your internet connection that is causing the error. So please check your network connection and make sure your internet is working properly. Apart from this, there could be other reasons behind this error, including conflicting subtitles add-on, unsupported subtitles for specific video content, and outdated Stremio app. like

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

If you encounter freezing issues when inserting hyperlinks into Outlook, it may be due to unstable network connections, old Outlook versions, interference from antivirus software, or add-in conflicts. These factors may cause Outlook to fail to handle hyperlink operations properly. Fix Outlook freezes when inserting hyperlinks Use the following fixes to fix Outlook freezes when inserting hyperlinks: Check installed add-ins Update Outlook Temporarily disable your antivirus software and then try creating a new user profile Fix Office apps Program Uninstall and reinstall Office Let’s get started. 1] Check the installed add-ins. It may be that an add-in installed in Outlook is causing the problem.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We
