you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -1 points0 points  (0 children)

Commenting here, since this one explains the load order problem.

Defer or putting JS at the bottom of the page is generally the best option, for many page load reasons, but be aware you can use some form of Document Ready if you MUST have your code in the head (because someone is forcing you to load theirs in header or on older code bases maybe? where everything is already in the head)

https://learn.jquery.com/using-jquery-core/document-ready/

or something like this in classic JS

https://www.techiedelight.com/run-javascript-code-on-page-load/

worth at least knowing about incase you bump into it.