forum

Home / DeveloperSection / Forums / Can I dyamically load multiple scripts by order

Can I dyamically load multiple scripts by order

Manoj Bhatt 1697 01-Feb-2015

I'm lazy loading various scripts depending on what page I'm on. Up till now, I've only loaded one per page. Now that I'm starting to clean up and reduce the number of scripts, I have to lazy load two scripts (or more) scripts for the same page. One script is dependent of the other so I need to load these in correct order.

What is the best way to achieve this?

My code:

// Plupload must load before imageHandling.js
jQuery(document).ready(function() {
    //Conditional load
    load_script('.image-drop-box','vendor/plupload/plupload.full.min.js');
    load_script('.image-drop-box','imageHandling.js');
 
});
 
 
// Script loader
// .exists() is a custom func that checks if an element exists on page
function load_script(element, js) {
    $(element).exists(function(){
        var script = document.createElement('script');
        var footer = document.getElementById('footer');
        script.type = 'text/javascript';
        script.src = '/js/'+ js;
        footer.appendChild(script);
    });
}


Updated on 01-Feb-2015

Can you answer this question?


Answer

1 Answers

Liked By