Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I continue executing my .js code after programmatically adding jQuery to the DOM?

How do I continue executing my .js code after programmatically adding jQuery to the DOM?

The 3rd answer in this post shows how to add jquery to the dom but how should you continue executing further code?

For example you can’t just add code underneath the self invoked function because jQuery hasn’t yet loaded. How do I continue writing my .js code so that it executes?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 $(window).on('load', function() {
    // jQuery hasn't yet loaded so can't use this yet.
 });

>Solution :

Execute the code that requires jQuery in the script’s load event listener.

var jQueryScript = document.createElement('script');
jQueryScript.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js');
jQueryScript.addEventListener("load", function() {
    $(document).ready(function() {
        ...
    });
});
document.head.appendChild(jQueryScript);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading