Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript cycle of execution

I've one script who is including another by this way :

 <script type="text/javascript" charset="UTF-8">
        //<![CDATA[
        ; (function (window, document, ud) {
            var AnId = 654654;
            var script                = document.createElement('script');
            script.setAttribute('charset', 'UTF-8');       
            script.setAttribute('src', '//something.com/blabla.js');
            script.setAttribute('type', 'text/javascript');
            document.getElementsByTagName('head')[0].appendChild(script);
        })(window, document);
    //]]>

       var AnId = 65777;
     </script>

My blabla.js must be executed right after being included to have the correct AnId value(654654) how can I deal with this execution order. Or is there another approach.

My final goal is to include several time the same script(with different id) without collision between them.

like image 906
Christophe Debove Avatar asked Sep 16 '26 15:09

Christophe Debove


2 Answers

script.async = false;

From https://developer.mozilla.org/en/docs/Web/HTML/Element/script:

Set this Boolean attribute to indicate that the browser should, if possible, execute the script asynchronously. It has no effect on inline scripts (i.e., scripts that don't have the src attribute).

like image 137
jgillich Avatar answered Sep 19 '26 05:09

jgillich


You can use onload event handler to create and run the second script:

script.onload = function() {
  runTheSecondScript();
};
like image 22
Volkan Ulukut Avatar answered Sep 19 '26 04:09

Volkan Ulukut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!