Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2004, 11:51 PM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default functions in <HEAD> vs <BODY>

Hi:

My first foray into Javascript.

I just had a simple question for which I've been unable to find an answer.

What are the criteria to consider when deciding where to place script: <HEAD> tags vs the <BODY> tags.

My specific situation deals with an onclick event for a checkbox, but I'm looking for a more general view.

Thanks,
JK
 
Old April 3rd, 2004, 05:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi JK,

IMO, you should try to put as much of your JavaScript in the <head> section of the page. That way, it's easy to maintain because all your code is packed together. Personally, I put all my JavaScript that goes in functions or in linked files in the <head> section, and only put JavaScript that needs to run immediately in the middle of the page. E.G.:
Code:
<html>
<head>
  <script type="text/javascript" language="JavaScript">
    function SayWhat(whatToSay)
    {
      alert(whatToSay);
    }
  </script>
</head>
<body onload="SayWhat('Pape has completed loading');">

... Page Here

  <script type="text/javascript" language="JavaScript">
    SayWhat('In the Middle of the Page');
  </script>

... Rest of Page Here

</body>
</html>
With this example, the "inline" script will run as soon as that part has finished loading (in the middle of the page), while all other code is placed in the <head> section.

So, IMO, a handler for the onclick of a checkbox should be placed in the <head> block because it's a separate function that can be called multiple times, and does not depend on being at a specific location in the page.

Does this help?

Imar



---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old April 3rd, 2004, 10:21 PM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Imar:

That makes perfect sense.

Thanks,

JK
 
Old April 5th, 2004, 08:32 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Also, even better in my opinion, is linking to a separate .js file like this:

...
<head>
...
<script language=javascript src=scripts.js>
...
</head>
...

This is better in my opinion because no matter how many pages you have that use this .js file, updating the .js file updates all the pages that use it.

----------
---Snib---
----------

<><
 
Old April 5th, 2004, 08:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, I couldn't agree more. For shared functionality, that is the best solution, as you'll have to develop your code only once and then reuse it across multiple pages.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 8: <asp:image> inside <a> & ext.CSS (pg. 274) epc BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 July 12th, 2008 04:37 AM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
Response.Write output is of <body></body> yoord BOOK: Beginning ASP.NET 1.0 0 October 13th, 2004 07:06 AM
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 07:01 AM
<STRONG> vs <B> and <EM> vs <I> anshul HTML Code Clinic 12 September 1st, 2004 05:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.