-1

I have the following in my template and it keeps coming back as $ is not defined in the console. However, if I wrap it in a setTimeout of a 500ms, it works fine. Is there a way to wait for Jquery to load without erroring?

$(window).on("load", function() {
  console.log("TEST")
});
herkypam
  • 75
  • 2
  • 11
  • 1
    Are you loading jQuery before your window.load code? – j08691 Jul 09 '19 at 20:02
  • @j08691 yea, the template is a PHP Blade file. Jquery is loaded in from the layout level so it's available on every template – herkypam Jul 09 '19 at 20:03
  • 1
    Is jQuery being loaded asynchronously? – Tyler Roper Jul 09 '19 at 20:15
  • @herkypam yes, that we assume, but where in the blade template is the code above and where is your jQuery library included? (maybe if you show us the blade template, things get more clear) – exside Jul 09 '19 at 20:18

1 Answers1

-1

That's because you don't have jquery, try adding <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> to your page.

You can see the generated source code in your browser ctrl + U and see if jquery is added or not, or with the dev tools

Yasiel Cabrera
  • 540
  • 4
  • 11
  • Please consider OP's comments on the question before submitting an answer. While there may be an issue with the manner in which they're loading jQuery, OP has said *"Jquery is loaded in from the layout level so it's available on every template"*. They've also said that a simple half-second timeout resolves the issue, so jQuery is being loaded *at some point*. – Tyler Roper Jul 09 '19 at 20:55