Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    placement of js functions on the page

    This may be a silly question, but do javascript functions have to be placed in the <head> of the html document? Can they be in the body instead?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by elyod_72 View Post
    This may be a silly question, but do javascript functions have to be placed in the <head> of the html document? Can they be in the body instead?
    A function acts as a definition for what to run when that function is called. In most cases you will want to pre-load the function code so that it is available as and when needed by the Javascript code you include in your web page. This means palcing the function code into the <head>. You can of course place the functions in the <body> if you prefer. The important thing is that the function is defined before you call it from your program.

    It is very common to call a script via a <body onload...> statement. In general scripts if placed in the <body> appear right after the <body> tag, but in some cases it may be desirable to place them right at the end, e.g. scripts that pre-load images used on other pages.

    Another point to consider when deciding whether to place your Javascript in the head or body section of the page is that if your code writes to the web page or affects the web page in any way then the statements to do that processing may only be included in the <body> section of the page or in functions within the <head> section of the page which are called from within the <body> of the page.

    Hope this helps.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    So, by short, javascript functions may be placed in head or in body as well; technically it does not matter.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Kor View Post
    So, by short, javascript functions may be placed in head or in body as well; technically it does not matter.
    Sorry, Kor. I don't agree.

    "Another point to consider when deciding whether to place your Javascript in the head or body section of the page is that if your code writes to the web page or affects the web page in any way then the statements to do that processing may only be included in the <body> section of the page or in functions within the <head> section of the page which are called from within the <body> of the page."

    For example, the code I supplied to canadianjameson in
    format variable output to use 'thousands' indicator (put a comma every 3 numbers)
    will not work in the <head> as it includes document.write.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    That code (and any other code) should simply not use document.write, as it is not a dynamic method. I always use DOM methods to create elements on the page, thus I may place my codes anywhere I like. Usually I prefere the head, but there are cases (includes) where I put them in the body.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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