-1

Solved: Using $.getScript works.

I have this script that will load 2 other scripts if you are on desktop. If you are not on desktop, those scripts won't load. This works fine.

(function () {
    // Detects devices
    if (isMobile.apple.device || isMobile.android.device || isMobile.seven_inch || isMobile.windows.device || isMobile.amazon.device) 
    {      
        console.log('Client is mobile. Not loading particles.');
        document.getElementById("warning-mobile").style.display = 'block'; 
    }
    else
    {  
        console.log('Client is desktop. Loading particles.');
        document.writeln("<script type='text/javascript' src='https://demihypercube.space/js/particles.js'></script>");
        document.writeln("<script type='text/javascript' src='https://demihypercube.space/js/app.js'></script>");
    }
})();

But, document.write is not the best method. Is there another way I can load particles.js and app.js? They need to load in that order.

1 Answers1

1

This is the general convention

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'url';    

document.getElementsByTagName('head')[0].appendChild(script);
Our_Benefactors
  • 3,220
  • 3
  • 21
  • 27