When doing dynamic sites, you sometimes have no choice but to put the JS in the body area. My rule of thumb is to use libraries wherever possible and include those libraries in the <head> section. But with the understanding that *sometimes* (though rarely these days) I will need to put JS into the body.

Take a look at what you are doing with the code that is in the body area. Can that code be wrapped up in a function and called in the $(document).ready() function? If so, then that is probably what you should be doing to make your code more self contained.

Building Ajax driven sites sometimes needs the code in the body area though. If you dynamically add a block of elements and need to do something to them the moment they are loaded, then this *might* be a reasonable time to embed JS code directly. Though I tend to use $.ajax() and so can just call a function after adding the elements. Which means I can pre-write that function in a library, with no JS needed in the body area.

Clear as mud?  :)

Shawn

breadwild wrote:
I have seen several examples on jQuery plugin sites that have the
<script> tag and Javascript within the <body> and not the <head> where
I thought it was supposed to go.

It would be better for my content manager and the templating system if
the Javascript *were* in the <body> but I don't want to be guilty of
not having unobtrusive code.

What's the standard/acceptable/best practices method?

<head>
   <script>
     javascript here
   </script>
</head>

VS.

<body>
   html here
   <script>
      javascript here
   </script>
</body>

Thanks in advance!

Reply via email to