0

all my jquery scripts has:

$(document).ready(function () { 

in the first line. I place them all in <head>

My question is: with this tag, are there any differences if I place this script in head or in the end of body?

RGS
  • 4,062
  • 4
  • 31
  • 67
  • In many cases, you won't need a `.ready()` function if you place the scripts after all other HTML content... – rnevius Oct 19 '15 at 19:18
  • http://stackoverflow.com/questions/436411/where-is-the-best-place-to-put-script-tags-in-html-markup – j08691 Oct 19 '15 at 19:21
  • Yes there is a difference. If you declare $(document).ready(function () in the head tag, it won't do anything since your document is represented in the body tag. http://stackoverflow.com/questions/17106462/whats-the-difference-between-putting-script-in-head-and-body – DinoMyte Oct 19 '15 at 19:21
  • 6
    @DinoMyte - That's 100% wrong. – j08691 Oct 19 '15 at 19:23
  • @DinoMyte body is part of document not vice versa – charlietfl Oct 19 '15 at 19:27
  • No. You just have to make sure jquery loads before using `$` anywhere – Shanimal Oct 19 '15 at 19:27
  • $(function() { is the same thing as $(document).ready(function () {? – RGS Oct 19 '15 at 19:36
  • @RickJoe yes it is http://api.jquery.com/ready/ – charlietfl Oct 19 '15 at 19:38
  • @charlietfl thank you! so I should delete this tag and place in on body? – RGS Oct 19 '15 at 19:39
  • 1
    doesn't matter where it is put other than jQuery.js must load first. The code within it won;t run until all the elements in body exist. That said...placing all script at end of body is preffered for faster page load – charlietfl Oct 19 '15 at 19:40

1 Answers1

0

There is no difference, as jQuery will only execute these functions when the DOM is ready. And that won't happen until browser fetches and parses the whole HTML.

But note that these functions will be executed in the same order they are defined in the document, so that can make a difference in many cases.

GiedriusT
  • 400
  • 3
  • 13