#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,545
    Rep Power
    221

    Load multiple JS files inside JS file


    Hi;

    How can I load multiple scripts with this?

    For example, now it is only https://code.jquery.com/jquery-3.3.1.min.js but I want to add https://cdnjs.cloudflare.com/ajax/li...uery.cookie.js too.

    Code:
    (function(window){ 
        var currentScript = document.currentScript; 
        var apiUrl = currentScript.src; 
    
        if (!('jQuery' in window)){ 
            loadJQuery(initialize); 
        } else { 
            initialize(); 
        }
    
        function loadJQuery(cb){ 
            var script = document.createElement('script'); 
            script.src = 'https://code.jquery.com/jquery-3.3.1.min.js'; 
            script.type = 'text/javascript'; 
            script.addEventListener('load', cb); 
            document.getElementsByTagName('head')[0].appendChild(script); 
    
        } 
    
        function initialize(){ 
            //Validate 
            var $currentScript = $(currentScript); 
            var params = $.param({ 
                action: 'load-template' 
                , launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed') 
                , launch_id: $currentScript.data('launch_id') 
            });
            
            $.get(apiUrl, params).then(function(html){ 
                var div = $('<div>').html(html); 
                $currentScript.after(div); 
            }); 
        } 
    }(this));
    I got this but its ugly as
    Code:
    (function(window) {
        var currentScript = document.currentScript;
        var apiUrl = currentScript.src;
    
    
        if (!('jQuery' in window)) {
            loadJQuery(initialize);
    
        } else {
            initialize();
        }
    
    
    
        function loadJQuery(cb) {
            var script = document.createElement('script');
            script.src = 'https://code.jquery.com/jquery-3.3.1.min.js';
            script.type = 'text/javascript';
            script.addEventListener('load', cb);
            document.getElementsByTagName('head')[0].appendChild(script);
        }
    
        function initialize() {
    
    
    
            $.getScript('https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js', function() {
                $(document).ready(function() {
                    console.log(123);
                    if (!$.cookie("timer_created_at")) {
                        var now = new Date().getTime();
                        $.cookie("timer_created_at", now);
    
                    } else {
                        var age = new Date().getTime() - $.cookie("timer_created_at");
                        console.log(age / 60000);
                    }
                });
            });
    
    
    
            var $currentScript = $(currentScript);
            var params = $.param({
                action: 'load-template',
                launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed'),
                launch_id: $currentScript.data('launch_id')
            });
    
            console.log('here');
    
    
    
    
            $.get(apiUrl, params).then(function(html) {
                var div = $('<div>').html(html);
                $currentScript.after(div);
            });
        }
    }(this));
    WhThere has to be a better way than
    Code:
    if (!('jQuery' in window)) {
            loadJQuery(initialize);
    
        } else {
            initialize();
        }
    Not sure why its done that way
    Last edited by English Breakfast Tea; June 19th, 2018 at 06:37 AM.

IMN logo majestic logo threadwatch logo seochat tools logo