# US 2021/0334113 A1 · Method and device for lazy loading of js scripts

> Agents: this data is available as JSON from the patentgrep API and MCP server — see https://patentgrep.com/llms.txt

- Number: US 2021/0334113 A1
- Document ID: US20210334113A1
- Status: Published application
- Kind: A1
- Application number: 16/486,351
- Filed: 2018-10-31
- Published: 2021-10-28
- Earliest claimed: 2018-10-19
- Assignees: Wangsu Science & Technology Co., Ltd.
- Applicants: Wangsu Science & Technology Co., Ltd.
- Inventors: Jiaxing ZHENG
- Web page: https://patentgrep.com/patent/US20210334113A1

## CPC

- G06F 9/44521: {Dynamic linking or loading; Link editing at or after load time, e.g. Java class loading}
- G06F 9/45529: {Embedded in an application, e.g. JavaScript in a Web browser}
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## Abstract

A method for lazy loading of JS scripts includes: determining whether a target browser supports a sequential execution function of an async attribute; if the target browser supports the sequential execution function of the async attribute, creating a script tag for each of the JS scripts based on a URL address for each of JS scripts to be lazily loaded, and setting async=false for each script tag; and sequentially inserting each script tag into an HTML document according to a script execution order of each of the JS scripts, so that the target browser downloads each of the JS scripts in parallel, and executes each of the JS scripts according to the script execution order.

## Claims

### Claim 1

A method for lazy loading of JS scripts, the method comprising:
determining whether a target browser supports a sequential execution function of an async attribute;
if the target browser supports the sequential execution function of the async attribute, creating a script tag for each of the JS scripts based on a URL address for each of JS scripts to be lazily loaded, and setting async=false for each script tag; and
sequentially inserting each script tag into an HTML document according to a script execution order of each of the JS scripts, so that the target browser downloads each of the JS scripts in parallel, and executes each of the JS scripts according to the script execution order.

### Claim 2 (depends on 1)

The method according to claim 1, wherein determining whether the target browser supports the sequential execution function of the async attribute further includes:
acquiring a predefined browser list recording browsers that do not support the sequential execution function of the async attribute; and
if the target browser exists in the predefined browser list, it is determined that the target browser does not support the sequential execution function of the async attribute, and if the target browser does not exist in the predefined browser list, it is determined that the target browser supports the sequential execution function of the async attribute.

### Claim 3 (depends on 1)

The method according to claim 1, further comprising:
if the target browser does not support the sequential execution function of the async attribute and the target browser is an IE browser, creating a script tag for each of the JS scripts based on the URL address of each to-be-lazily loaded JS script, and placing each script tag in a queue according to the script execution order, so that the target browser downloads each of the JS scripts in parallel;
when a monitored script download status for a JS script corresponding to a script tag located at the beginning of the queue indicates “download completed”, inserting the script tag located at the beginning of the queue into the HTML document, so that the target browser executes the JS script corresponding to the script tag located at the beginning of the queue; and
when a monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, inserting a script tag next to the script tag located at the beginning of the queue into the HTML document.

### Claim 4 (depends on 1)

The method according to claim 1, further comprising:
if the target browser does not support the sequential execution function of the async attribute and the target browser is a non-IE browser, creating an image tag for downloading each to-be-lazily loaded JS script based on the URL address of each to-be-lazily loaded JS script, so that the target browser downloads each to-be-lazily loaded JS script in parallel based on the created each image tag;
creating a script tag for each of the JS scripts based on the URL addresses of each to-be-lazily loaded JS script, and placing each script tag into a queue according to the script execution order;
when a monitored script download status for a JS script corresponding to a script tag located at the beginning of the queue indicates “download completed”, inserting the script tag located at the beginning of the queue into the HTML document, so that the target browser executes the JS script corresponding to the script tag at the beginning of the queue; and
when a monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, inserting a script tag next to the script tag located at the beginning of the queue into the HTML document.

### Claim 5 (depends on 3)

The method according to claim 3, wherein inserting the script tag next to the script tag located at the beginning of the queue into the HTML document when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed” further includes:
when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, deleting the script tag located at the beginning of the queue from the queue; and
when a monitored script download status for a JS script corresponding to a script tag presently located at the beginning of the queue indicates “download completed”, inserting the script tag presently located at the beginning of the queue into the HTML document.

### Claim 6 (depends on 1)

The method according to claim 1, further comprising:
if the target browser does not support the sequential execution function of the async attribute, if the target browser is a non-IE browser, and if to-be-lazily loaded JS scripts are same-domain scripts, creating a script tag for each of the JS scripts based on the URL address of each to-be-lazily loaded JS script;
placing each script tag into a queue according to the script execution order, and initiating an ajax download request for each script tag, so that the target browser downloads each of the JS scripts in parallel;
when a monitored script download status for a JS script corresponding to a script tag located at the beginning of the queue indicates “download completed”, initiating an eval execution request for the JS script corresponding to the script tag located at the beginning of the queue; and
when a monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, initiating an eval execution request for a JS script corresponding to a script tag next to the script tag located at the beginning of the queue.

### Claim 7 (depends on 6)

The method according to claim 6, wherein initiating an eval execution request for the JS script corresponding to the script tag next to the script tag located at the beginning of the queue when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed” further includes:
when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, deleting the script tag located at the beginning of the queue from the queue; and
when a monitored script download status for a JS script corresponding to a script tag presently located at the beginning of the queue indicates “download completed”, initiating an eval execution request for the JS script corresponding to the script tag presently located at the beginning of the queue.

### Claim 8 (depends on 1)

The method according to claim 1,
wherein, before determining whether the target browser supports the sequential execution function of the async attribute, the method further includes:
if the target browser supports a preload function of a <link> tag, creating a <link> tag for each of the JS scripts based on the URL address of each to-be-lazily loaded JS script; and
inserting each <link> tag into a head portion of the HTML document according to the script execution order, so that the target browser downloads each of the JS scripts in parallel based on each <link> tag.

### Claim 9

A device for lazy loading of JS scripts, the device comprising:
a decision-making module that is configured to determine whether a target browser supports a sequential execution function of an async attribute;
a controlling module that is configured to, if the target browser supports the sequential execution function of the async attribute, create a script tag for each of the JS scripts based on a URL address of each to-be-lazily loaded JS script, and set async=false for each script tag; and
an executing module that is configured to sequentially insert each script tag into an HTML document according to a script execution order of each of the JS scripts, so that the target browser downloads each of the JS scripts in parallel, and executes each of the JS scripts according to the script execution order.

### Claim 10 (depends on 9)

The device according to claim 9, wherein the decision-making module is further configured to:
acquire a predefined browser list recording browsers that do not support the sequential execution function of the async attribute; and
if the target browser exists in the predefined browser list, it is determined that the target browser does not support the sequential execution function of the async attribute, and if the target browser does not exist in the predefined browser list, it is determined that the target browser supports the sequential execution function of the async attribute.

### Claim 11 (depends on 9)

The device according to claim 9, wherein
the controlling module is further configured to:
if the target browser does not support the sequential execution function of the async attribute and the target browser is an IE browser, create a script tag for each of the JS scripts based on the URL address of each to-be-lazily loaded JS script, and place each script tag in a queue according to the script execution order, so that the target browser downloads each of the JS scripts in parallel; and
the executing module is further configured to:
when a monitored script download status for a JS script corresponding to a script tag located at the beginning of the queue indicates “download completed”, insert the script tag located at the beginning of the queue into the HTML document, so that the target browser executes the JS script corresponding to the script tag located at the beginning of the queue, and
when a monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, insert a script tag next to the script tag located at the beginning of the queue into the HTML document.

### Claim 12 (depends on 9)

The device according to claim 9, wherein
the controlling module is further configured to:
if the target browser does not support the sequential execution function of the async attribute and the target browser is a non-IE browser, create an image tag for downloading each to-be-lazily loaded JS script based on the URL address of each to-be-lazily loaded JS script, so that the target browser downloads each of the JS scripts in parallel based on the created each image tag, and
create a script tag for each of the JS scripts based on the URL addresses of each to-be-lazily loaded JS script, and place each script tag into a queue according to the script execution order; and
the executing module is further configured to:
when a monitored script download status for a JS script corresponding to a script tag located at the beginning of the queue indicates “download completed”, insert the script tag located at the beginning of the queue into the HTML document, so that the target browser executes the JS script corresponding to the script tag located at the beginning of the queue, and
when a monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, insert a script tag next to the script tag located at the beginning of the queue into the HTML document.

### Claim 13 (depends on 11)

The device according to claim 11, wherein the executing module is further configured to:
when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, delete the script tag located at the beginning of the queue from the queue; and
when a monitored script download status for a JS script corresponding to the script tag presently located at the beginning of the queue indicates “download completed”, insert the script tag presently located at the beginning of the queue into the HTML document.

### Claim 14 (depends on 9)

The device according to claim 9, wherein
the controlling module is further configured to:
if the target browser does not support the sequential execution function of the async attribute, if the target browser is a non-IE browser, and if the to-be-lazily loaded JS scripts are same-domain scripts, create a script tag for each of the JS scripts based on the URL address of each to-be-lazily loaded JS script, and
place each script tag into a queue according to the script execution order, and initiate an ajax download request for each script tag, so that the target browser downloads each of the JS scripts in parallel; and
the executing module is further configured to:
when a monitored script download status for a JS script corresponding to a script tag located at the beginning of the queue indicates “download completed”, initiate an eval execution request for the JS script corresponding to the script tag located at the beginning of the queue, and
when a monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, initiate an eval execution request for a JS script corresponding to a script tag next to the script tag located at the beginning of the queue.

### Claim 15 (depends on 14)

The device according to claim 14, wherein the executing module is further configured to:
when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, delete the script tag located at the beginning of the queue from the queue; and
when a monitored script download status for a JS script corresponding to a script tag presently located at the beginning of the queue indicates “download completed”, initiate an eval execution request for the JS script corresponding to the script tag presently located at the beginning of the queue.

### Claim 16 (depends on 9)

The device according to claim 9, further comprising a preloading module that is configured to:
if the target browser supports a preload function of a <link> tag, create a <link> tag for each of the JS scripts based on the URL addresses of each to-be-lazily loaded JS script; and
insert each <link> tag into a head portion of the HTML document according to the script execution order, so that the target browser downloads each of the JS scripts in parallel based on each <link> tag.

### Claim 17

A terminal, comprising: a processor and a memory, wherein the memory stores at least one instruction, at least one program, a code set, or an instruction set, and the at least one instruction, the at least one program, the code set, or instruction set is loaded and executed by the processor to implement a method for lazy loading of JS scripts, the method comprising:
determining whether a target browser supports a sequential execution function of an async attribute;
if the target browser supports the sequential execution function of the async attribute, creating a script tag for each of the JS scripts based on a URL address for each of JS scripts to be lazily loaded, and setting async=false for each script tag; and
sequentially inserting each script tag into an HTML document according to a script execution order of each of the JS scripts, so that the target browser downloads each of the JS scripts in parallel, and executes each of the JS scripts according to the script execution order.

### Claim 18

(canceled)

### Claim 19 (depends on 4)

The method according to claim 4, wherein inserting the script tag next to the script tag located at the beginning of the queue into the HTML document when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed” further includes:
when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, deleting the script tag located at the beginning of the queue from the queue; and
when a monitored script download status for a JS script corresponding to a script tag presently located at the beginning of the queue indicates “download completed”, inserting the script tag presently located at the beginning of the queue into the HTML document.

### Claim 20 (depends on 12)

The device according to claim 12, wherein the executing module is further configured to:
when the monitored script execution status for the JS script corresponding to the script tag located at the beginning of the queue indicates “execution completed”, delete the script tag located at the beginning of the queue from the queue; and
when a monitored script download status for a JS script corresponding to the script tag presently located at the beginning of the queue indicates “download completed”, insert the script tag presently located at the beginning of the queue into the HTML document.
