What are the best javascript loading methods with minimal impact on screen performance?

Is there a way to load javascript without executing it? I want to reduce page load time, so I try to "lazy load" the same amount of javascript on the page while the user is idle. However, I do not want javascript to be executed, I just want it to be in the browser cache.

Should I use an object tag? I noticed that I can use the LINK tag, but this makes the browser think it is css, which negatively affects my performance / responsiveness.

+3
source share
4 answers

You can load the file using the XMLHttpRequest object in JavaScript. (Aka AJAX). (then, of course, just discard the result ^^).

0

, .

script

window.load(function() {  //your initialisation here });

.

script script, , .

script , script, . :

http://unixpapa.com/js/dyna.html

http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

, , .

+2

, - JavaScript . javascript - , , . , // jQuery jQuery script; jQuery, jQuery . . , .

, , JS </body>. , JS , CSS .

+1

The best way to handle external javascript is to load it after everything else on the page by placing it at the bottom of the page. Then everything that can be displayed will be displayed, and then javascript at the bottom of the page will load and compile and cache. Of course, this only works if javascipt is a library of functions that should not be executed on the middle page, in which case you are stuck in compiling and batch loading javascript independently.

Require.JS is a great library for automatic control when loading javascript.

+1
source

Source: https://habr.com/ru/post/1790237/


All Articles