0

ok, lets load jQuery at the bottom. But I need it sooner than that:

<body> <div id="aaaa"></div> <script> $('#aaa').click FAIL </script> </body> <script src=jquery> 
3
  • 1
    Then load it as the first script in the <head></head> section? Commented Oct 29, 2015 at 15:37
  • 1
    Then load it in the head of the document..? Commented Oct 29, 2015 at 15:37
  • 1
    Seems a bit over the top to ask this trivial question twice within half an hour. You simply can't use jQuery before it's loaded. And there's always vanilla JS as an option. stackoverflow.com/q/33417000/3168107 Commented Oct 29, 2015 at 16:03

3 Answers 3

2

(function(){click Event here}); Last step jQuery append as Last Element for the Body for faster loading

Extern scripts for more performance und er the jquery Core

Sign up to request clarification or add additional context in comments.

Comments

1

Even if some people don't recommend it, I do recommend loading jQuery as the first script in your website. The jQuery plugins and stuff can go at the bottom of your document without problems if you add non-intrusive code after loading them.

Instead of loading at the bottom of the <body></body> part of the document, load it in the header:

<html> <head> <title>...</title> <script type="text/javascript" src="jquery_file.js"></script> <!-- ... --> </head> <body> <!-- ... --> <a href="#" id="aaa">Click me</a> <!-- ... --> <script type="text/javascript"> $('#aaa').click(function(e) { alert('Now this should work.'); }); </script> </body> </html> 

As a sidenote, please consider not including any script inside the <body> tag. jQuery is made to be, as I said before, non-intrusive, meaning it should not be mixed with the HTML. You're doing bad practices.

Your $('#aaa').click could (and should) be called in a script outside of the body tag itself and after importing jQuery. You could perfectly include that script tag just below the one that imports the jQuery library and it should work just fine.

Comments

1

Either move the jquery tag up, or put the other script tag also below under where you include jquery

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.