Where do I put the <script> code.

Discussion in 'HTML & Website Design' started by mcfc4eva, Mar 28, 2009.

  1. #1
    Does it matter where in a XHTML document I place the following code;

    Does it HAVE to be in the <head> or can it be in the <body> and does it have to be at the beginning of those nodes or can it be anywhere within them? What effects will it have by being in different places?

    Thanks,
    Michael
     
    mcfc4eva, Mar 28, 2009 IP
  2. Oxi

    Oxi Peon

    Messages:
    78
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It depends on the purpose of the script. The majority of scripts go in the header or rather the best way is to declare the script in the header referencing to a script file on your host. I have used scripts before that had a specific purpose that are declared in the body tag but as far as im aware, its rare.
    You can place the script statement anywhere between the <head> and </head> tags and it won't make a difference.
     
    Oxi, Mar 28, 2009 IP
  3. graphito

    graphito Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    right, it's in header :p
     
    graphito, Mar 28, 2009 IP
  4. mcfc4eva

    mcfc4eva Well-Known Member

    Messages:
    602
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    110
    #4
    why does it seem to work in the <body>. It's a script to process a form and calculate a few numbers.
     
    mcfc4eva, Mar 28, 2009 IP
  5. Oxi

    Oxi Peon

    Messages:
    78
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Its all about when you want the script to load. When your html file loads the body is executed. So as the page loads the script runs. By placing it in the header the script does not run until you trigger it. It is better practice to use the script declaration in the header tag. Also, as i was saying, you may want to get into the habbit of referencing to a script file on your host.

    <script type="text/javascript" src="yourfile.js"></script>
     
    Oxi, Mar 28, 2009 IP
  6. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #6
    Script declarations are best put in the header. Script triggers must be in the body at the correct location.

    Referencing your common script files residing on your host server is good policy as they can be edited without editing every page they occur on. However, some sites, notably eBay, won't allow you to reference offsite scripts, so in those cases your script declarations should be in the header.
     
    mmerlinn, Mar 29, 2009 IP
  7. m3rlinez

    m3rlinez Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It's okay to put your <script> code in the <body>. Takes Google Analytics code as example. However, it is a good practice to put everything in the <head> section.
     
    m3rlinez, Mar 29, 2009 IP
  8. anhbloginc

    anhbloginc Well-Known Member

    Messages:
    1,288
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    175
    #8
    You can put anything you want. It will run away.
     
    anhbloginc, Mar 29, 2009 IP