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>
John Smith
  • 6,129
  • 12
  • 68
  • 123

3 Answers3

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

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.

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
1

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

Saransh Kataria
  • 1,447
  • 12
  • 19