0

I have a big inline jQuery function that I want to trigger after the jQuery library is loaded at the bottom. Otherwise I'd get the "Uncaught ReferenceError". Therefore I wrapped the function into a timeout-function - which works, but only as long as the DOM is loaded faster than the timout takes. I already tried window-on-load-function, whicht gives me back "Uncaught ReferenceError" as well.

HTML-Document looks like this:

<html>
<head>
...
</head>
<body>
...
<script>

    // Inline jQuery that needs to stay inline

    setTimeout( function(){ 

        // Run my big jQuery function

    }, 100 );

</script>
...
<script src="jQuery-library.js" ><script>
<script src="my-jQuery-functions.js" ><script>
</body>

Is there any way to trigger my inline-function after the jQuery-library in the documents bottom is loaded completely, and possibly WITHOUT moving jQuery-library to the head? Thank you very much in advance!

1
  • Why can't you just move the script below jQuery?
    – Barmar
    Commented Oct 19, 2020 at 21:13

1 Answer 1

2

You can react to the load event, which occurs after all resources (scripts, images, etc) have finished loading:

<script>
window.addEventListener("load", function () {
  $(function () {
      $("body").append("<p>jQuery is available now!</p>");
      
      // Run my big jQuery function
  });
}, false);
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

1
  • WOW - Thank you sooo much! That saved me a lot of headache, and is super simple at the same time! Great, thanks again! Commented Oct 19, 2020 at 21:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.