


Implementation principles of synchronous loading and asynchronous loading of javascript files_javascript skills
charset: Optional. Specifies the character set of the code introduced by src. Most browsers ignore this value.
defer: boolean, optional. Delaying script execution is equivalent to placing the script tag at the bottom of the body tag of the page. The js script will be executed before DOMContentLoaded of the document. Except for IE and newer versions of Firefox, other browsers are not supported.
language: Deprecated. Most browsers ignore this value.
src: optional. Specify the imported external code file, without limiting the suffix name.
type: required. Specifies the content type (MIME type) of the script. In reality, it is usually not necessary to specify this value. The browser will interpret and execute it as text/javascript type by default.
script attribute in HTML5 : In addition to the attributes defined by the new HTML5 standard, the
script tag in HTML5 has removed the language attribute and modified it compared to HTML4.01. The type attribute is optional (default text/javascript), and a new attribute async is added.
async: boolean, the role of the attribute, defines whether the script is executed asynchronously, the value is true or false.
If async is set to true , the defer attribute will be ignored.
Asynchronously executed js files are assumed not to use document.write() to write content to the loading document, so do not use document.write() during the loading and execution of asynchronously executed js files
Except In addition to the script tag attribute, the way the page introduces the js file affects its loading and execution mode:
Any js file introduced by adding a script node (such as appendChild(scriptNode)) is executed asynchronously (scriptNode needs to be inserted into the document, Just creating the node and setting src will not load the js file, which is not analogous to the preloading of img)
The code in the <script> tag in the html file or the code in the js file referenced by src is loaded synchronously And the code in the <script> tag in the executed <br>html file uses the document.write() method to introduce the js file which is referenced by the src attribute of the <script> tag in the <br>html file that is executed asynchronously. The js file introduced using the document.write() method in the code of the js file is executed synchronously <br>Use the Image object to asynchronously preload the js file (will not be executed) <br><br>Do not use something like the following This method will not initiate a request to load the js file: <br>divNode.innerHTML = '<script src="xxx.js"></script>';
window.onload event will be executed in js Triggered only after the file is loaded (even if it is loaded asynchronously)
====================================== =================
1.
<script> <br>//Synchronously load the executed code <br></script>
2.
//Synchronously load and execute the code in xx.js
3.
<script> <br>document. write('<script src="xx.js"></script>'); //Asynchronously load and execute the code in xx.js
4,
xx.js contains the following code:
document.write('');
document.write('< ;script src="22.js">');
Then xx.js, 11.js, and 22.js are all loaded and executed synchronously.
If xx.js, 11.js and 22.js are loaded asynchronously by inserting script nodes, then 11.js and 22.js are loaded asynchronously.
If xx.js is loaded asynchronously by inserting script nodes, 11.js and 22.js are loaded in document.write(script) mode, then 11.js and 22.js are loaded synchronously (tested by the latest browser, under chrome, the asynchronous loading execution of xx.j is no longer available document.write() writes content to the document, but firefox and IE can write before the document is closed (the method is to use alert in html to prevent the document from closing))
Test: alert() in 11.js ( Do not use a for loop, the browser is executed in a single thread, and continuing to execute any piece of code will cause the rest of the code to be blocked), console.log() in 22.js, you can see that the code in 22.js is blocked
5.
In the following method, xx.js will be loaded and executed asynchronously after appendChild is executed
var script = document.createElement("script");
script.setAttribute("src","xx.js");
documenrt.getElementsByTagName("head") [0].appendChild(script);
6. Use the Image object to asynchronously preload js files (will not be executed)
The request is initiated when the src of the Image is assigned, and the file type is not picky (the image may also be dynamically created by a script, such as Verification code), so you can assign the url of the js file to image.src, and the js will be cached by the browser after loading.
var img = new Image();
img.onload = function(){ alert(1); }; //Since the returned js file MIME is not an image, The onload callback function will not be triggered
img.src = 'http://localhost/test/loadjs/try.2.js';
var s = document.createElement("script");
var h = document.getElementsByTagName("head")[0];
//Execute js
s.src=img.src;
h.appendChild(s);
A function to load js files:
var loadJS = function(url,callback){
var head = document.getElementsByTagName('head');
if(head&&head.length){
head = head[0];
}else{
head = document.body;
}
var script = document.createElement('script');
script.src = url;
script.type = "text/javascript";
head.appendChild( script);
script.onload = script.onreadystatechange = function(){
//Script tag, IE has onreadystatechange event, w3c standard has onload event
//These readyState It is for IE8 and below. The W3C standard script tag does not have onreadystatechange and this.readyState.
//Onload will not be executed if the file is not loaded successfully.
//(!this.readyState) is for the W3C standard. IE 9 also supports the W3C standard onload
if ((!this.readyState) || this.readyState == "complete" || this.readyState == "loaded" ){
callback();
}
}//end onreadystatechange
}
For the test of point 4 (synchronous loading) (inserting alert makes it easy to see the blocking during loading)
tryjs .html
< ;html>
tryjs .js
console.log('write begin');
document.write('');
document.write('');
console.log('write finished');
try.1.js
console.log('loadjs 1 begin');
console.log('loadjs 1 finished');
try.2.js
console.log('loadjs 2 begin');
console.log('loadjs 2 finished');
Test results (the order of callback complete of file 2 and file 1 is uncertain in IE789)
IE 7:
Log: outer js callback loading IE
Log: outer js callback loaded IE
Log : write begin
log: write finished
log: outer js callback complete IE
log: file 1 callback loading IE
log: file 2 callback loading IE
log: loadjs 1 begin
Log: loadjs 1 finished
Log: loadjs 2 begin
Log: loadjs 2 finished
Log: file 2 callback complete IE
Log: file 1 callback complete IE
IE8:
Log: outer js callback loading IE
Log: outer js callback loaded IE
Log: write begin
Log: write finished
Log: outer js callback complete IE
Log: file 1 callback loading IE
log: file 2 callback loading IE
log: loadjs 1 begin
log: loadjs 1 finished
log: loadjs 2 begin
log: loadjs 2 finished
log : file 2 callback complete IE
log: file 1 callback complete IE
IE9:
log: write begin
log: write finished
log: outer js callback complete IE
Log: file 1 callback loading IE
Log: file 2 callback loading IE
Log: loadjs 1 begin
Log: loadjs 1 finished
Log: loadjs 2 begin
Log: loadjs 2 finished
Log: file 1 callback complete IE
Log: file 2 callback complete IE
FIREFOX:
write begin
write finished
outer js callback, not IE
loadjs 1 begin
loadjs 1 finished
file 1 callback,NOT IE
loadjs 2 begin
loadjs 2 finished
file 2 callback,NOT IE
CHROME:
write begin
write finished
outer js callback, not IE
loadjs 1 begin
loadjs 1 finished
file 1 callback,NOT IE
loadjs 2 begin
loadjs 2 finished
file 2 callback,NOT IE

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

The secret of Ajax anomaly is revealed. How to deal with various errors requires specific code examples. In 2019, front-end development has become an important position that cannot be ignored in the Internet industry. As one of the most commonly used technologies in front-end development, Ajax can realize asynchronous page loading and data interaction, and its importance is self-evident. However, various errors and exceptions are often encountered when using Ajax technology. How to deal with these errors is a problem that every front-end developer must face. 1. Network errors When using Ajax to send requests, the most common error is

Title: Methods and code examples to solve the problem that jQuery.val() does not work. In front-end development, jQuery is often used to operate page elements. Among them, getting or setting the value of a form element is one of the common operations. Usually, we use jQuery's .val() method to operate on form element values. However, sometimes you encounter situations where jQuery.val() does not work, which may cause some problems. This article will introduce how to effectively deal with jQuery.val(

Scrapy is an open source Python crawler framework that can quickly and efficiently obtain data from websites. However, many websites use Ajax asynchronous loading technology, making it impossible for Scrapy to obtain data directly. This article will introduce the Scrapy implementation method based on Ajax asynchronous loading. 1. Ajax asynchronous loading principle Ajax asynchronous loading: In the traditional page loading method, after the browser sends a request to the server, it must wait for the server to return a response and load the entire page before proceeding to the next step.

Detailed explanation of the suspend function in Vue3: Optimizing asynchronous data loading In modern websites and applications, asynchronous data loading is essential. However, due to the instability of network connection speeds, asynchronous data loading may cause delays and freezes in the user interface. To solve this problem, Vue3 introduces a new suspend function to optimize asynchronous data loading. The suspense function is a new feature in Vue3. It allows you to display a loading UI when loading data asynchronously until asynchronously.

Although HTML itself cannot read files, file reading can be achieved through the following methods: using JavaScript (XMLHttpRequest, fetch()); using server-side languages (PHP, Node.js); using third-party libraries (jQuery.get() , axios, fs-extra).

Detailed explanation of the defineAsyncComponent function in Vue3: Application of asynchronous loading components In Vue3, we often encounter the need to load components asynchronously. At this time, we can use the defineAsyncComponent function provided by Vue3 to implement the function of loading components asynchronously. This article will introduce in detail the usage and application scenarios of the defineAsyncComponent function in Vue3. 1. defineAsyncComponent

Delegation is a type-safe reference type used to pass method pointers between objects to solve asynchronous programming and event handling problems: Asynchronous programming: Delegation allows methods to be executed in different threads or processes, improving application responsiveness. Event handling: Delegates simplify event handling, allowing events such as clicks or mouse movements to be created and handled.

How to prevent page redirection in WordPress? In website development, sometimes we want to implement a page non-jump setting in WordPress, that is, during certain operations, the page content can be updated without refreshing the entire page. This improves user experience and makes the website smoother. Next, we will share how to implement the page non-jump setting in WordPress and provide specific code examples. First, we can use Ajax to prevent the page from jumping. Ajax
