Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 2nd, 2004, 11:51 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
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
Reply With Quote
  #2 (permalink)  
Old April 3rd, 2004, 05:47 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #3 (permalink)  
Old April 3rd, 2004, 10:21 PM
Authorized User
 
Join Date: Jun 2003
Location: Sheridan, OR, USA.
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Imar:

That makes perfect sense.

Thanks,

JK
Reply With Quote
  #4 (permalink)  
Old April 5th, 2004, 08:32 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
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---
----------

<><
Reply With Quote
  #5 (permalink)  
Old April 5th, 2004, 08:53 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:48 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.