There is such a JavaScript:

$(document).ready(function(){ if ($('a.lesson-audio-play.active').length > 0) { // include jwPlayer if ($('script[src^="core/modules/jwplayer/jwplayer.js"]').length === 0) { var s = document.createElement('script'); s.src = 'core/modules/jwplayer/jwplayer.js'; // s.defer = 'defer'; Нужно ли это? $('head').append(s); } } }); 

How correct? it is desirable that the script be without delay, otherwise some functionality will not work. Or is it generally better to use async and why?

    1 answer 1

    Taken from here
    The defer attribute tells the script to be executed only when the entire $(document).ready() document is loaded.
    The async attribute tells the script to execute it immediately as soon as it loads. For example:

     function addScript(src){ var script = document.createElement('script'); script.src = src; script.async = false; // чтобы гарантировать порядок document.head.appendChild(script); } addScript('1.js'); // загружаться эти скрипты начнут сразу addScript('2.js'); // выполнятся, как только загрузятся addScript('3.js'); // но, гарантированно, в порядке 1 -> 2 -> 3 

    if script.async = true; then the script that loads first and the first one is executed.
    In your case, if you load scripts and want to execute them immediately (as soon as they are loaded), use script.async = true