Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

behnampmdg3's avatar

Load multiple scripts inside Javascript

Omg, I am going mad.

I am loading Jquery, works sweet.

But I also want to load

https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js

But I can't. No matter what I try it fails.

Help plz.

(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)); 
0 likes
4 replies
Thyrosis's avatar

Might be thinking out of the box here, but can't you just include

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>

in the HTML instead of using JS-functions to load something?

behnampmdg3's avatar

It's not like that. The page is not html

 <?php 
header('Access-control-allow-origin: *'); 
$action = isset($_GET['action'])?$_GET['action']:null; 
switch ($action){ 
    case 'load-template': 
        load_template(); 
        break; 
    default: 
        DoDefault();  
} 
exit; 
function DoDefault(){ 
    header('Content-type: text/javascript'); 
?> 
(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)); 
<?php 
} 
function validate() 
    { 
       
    } 

  function load_template() 
      { 
      
      }
behnampmdg3's avatar

I have this already but it's ugly as F

(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));
NickVahalik's avatar

Are you sure that $ is there when you are calling initialize? Here is a helpful snippet that can bring in another script: https://snipplr.com/view/18756/loadscript/

You might need to call .noConflict() and assign jQuery to a variable and then pass it into your other scripts.

Please or to participate in this conversation.