0

My question is simple, but I have not found any answer for this.

1. Where should I place the jquery library? in the body or head? I'm asking about the jquery library, not about my jquery's.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

2. Is there any problem if I put MY jquery's in the end of body with:

$( document ).ready(function() {

? I know I need this in the head, not in the body, what I want to know is if it can slow my page or something like this, because I want to leave this tag if in the future I decided to place it in head again.

RGS
  • 4,062
  • 4
  • 31
  • 67

1 Answers1

1

Best practices recomends:

  • Libraries goes to head section.
  • Your jquery code should go to head section inside <script> tag or external jsfile.

You have to place your jQuery code after the jQuery library

you can place your <script> tags at the top or bottom of the page. Some users used to place the tag at the bottom to ensure the page is rendered when control reach the script code, but for that it's recommended you use the ready jQuery event placed at page top.

anmarti
  • 5,045
  • 10
  • 55
  • 96
  • thank you friend! so I will put the jquery library in the head and my jquery's in the bottom! – RGS Oct 20 '15 at 11:20
  • It's not necesary to put your code at the bottom. Put it at head using correctly the `ready` event. – anmarti Oct 20 '15 at 11:21
  • isn't it faster if I put it at the bottom? – RGS Oct 20 '15 at 11:23
  • 1
    If you are putting scrips on the head of your site, don't forget to use async or defer, otherwise this aproach is worse than putting at the bottom. – madagalbiati Oct 20 '15 at 11:24
  • 1
    Check this, it explains: http://stackoverflow.com/questions/855077/does-putting-scripts-on-the-bottom-of-a-web-page-speed-up-page-load – anmarti Oct 20 '15 at 11:25
  • @madagalbiati thank you! I will place async in my head scripts! – RGS Oct 20 '15 at 11:31