performance - Download javascript file but not evaluate it immediately? -


i have large javascript file use during web page loading. hope can downloaded possible (download not block ui thread) not evaluated (loading in memory in ui thread , blocks domcontentcomplete event , initial page paint).

it init xhr additional data , update page. users see static page , data little later. if in html head, initial static page display delayed. if @ page end, download delay whole content display.

is there way download not parse until initiate parse later on?

if js file isn't critical rendering can use async or defer attribute on <script> tag file. using async have same effect placing <script> tag right before closing <body> tag.

< script > - mdn

async

html5 set boolean attribute indicate browser should, if possible, execute script asynchronously. has no effect on inline scripts (i.e., scripts don't have src attribute).

defer

this boolean attribute set indicate browser script meant executed after document has been parsed. since feature hasn't yet been implemented other major browsers, authors should not assume script’s execution deferred.

you possibly use settimeout call. long <script> isn't blocking rendering, wrapping in settimeout should delay you.

var delay = 10 /* or many ms want */ settimeout(function(){     /* code goes here */ }, delay) 

edit: if you're looking execute after domcontentloaded event, wrap code in event listener event.

 document.addeventlistener("domcontentloaded", function(event) {     /* code goes here */   }); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -