SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why does some javascript go in head tag?

    Why does some javascript go in the head tag,and some goes in the body tag? What makes the difference with where it needs to go in the code?

    Thanks!

  2. #2
    SitePoint Enthusiast RogueJedi's Avatar
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it really doesn't matter where you put most JavaScript code -- in the <head>, <body>, or just before the </html> tags.

    It has become standard practice, tho', to put certain types of code in certain places. For exaple:

    The following would normally go in the <head> section of an HTML document:
    Code:
    <script language="JavaScript" type="text/javascript">
    function doStuff {
        document.write("<p>Some Stuff</p>"):
    }
    </script>
    While the following would be put into the document body at the position where you would want to output to show up:
    Code:
    <p>
    <script language="JavaScript" type="text/javascript">
    var newVar = "Some Stuff";
    document.write(newVar);
    </script>
    </p>
    Ultimately, it's just a matter of style -- how do you prefer to do it?
    Rob Nolan
    Do or do not, there is no try.

  3. #3
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also find it is a matter of neatness. I put everything possible in the head and call it if I have to from the body. This way when another coder comes along he can read the code quite a bit easier than having tonnes of <script> tags throughout the body.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    UK
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose it doesn't matter, as long as your function is defined before your call to it, is called. If you kno what I mean.

    I reckon some is put in the head because the authors want it to load first.

  5. #5
    SitePoint Enthusiast RogueJedi's Avatar
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I generally try to keep as much of my scripts as possible in the head section, or in an external file. Especially if I have a lot of functions, or if any of the functions are more than 10 lines of code.
    Rob Nolan
    Do or do not, there is no try.

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi MarnieB,

    As a general rule, javascript code goes between the head so they are loaded and ready for when the page is rendered. (Microsoft's FrontPage Xpress places all javascript code directly after the <body> tag; then again, they do a lot of weird stuff)

    The only real rules you need to follow are:
    1) never use a function before it is declared:
    x = myFunc(1,3);
    function myFunc(a, b).....
    2) never call a function that uses form elements -- until the form has loaded.
    function myFunc()
    {
    document.myForm.myTextBox.value = 'Hi';
    }
    otherwise, you'll get 'has no properties' errors.
    3)never put HTML tags within an external *.js file -- this includes <script> tags
    4) avoid putting <script> tags around every function
    it makes for messy code and takes a nanosecond extra to load

    Vinny
    Last edited by Vincent Puglia; Nov 20, 2001 at 22:22.
    Where the World Once Stood
    the blades of grass
    cut me still

  7. #7
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'ld have to agree with Vincent Puglia. It a good idea to place javascript in the head of the docuemnt as for it to be already loaded when the user tried to call it. There would be no point if the call for a function is loaded to the page before the actual function.

    What if someone caused an instance of the function before it was loaded? It was cos an error and wouldn't look very good, would it.

    But thats just my 2cents



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •