Difference between places of <script> tags in HTML?

Discussion in 'JavaScript' started by ignas2526, Jul 18, 2008.

  1. #1
    I have question:
    is there difference between putting <script> tags between <head></head> and <body></body> tags ?
    because i tried to move some scripts out of head, and back, and all was same....
    ----------
    Also, all my pages includes over 10 same JavaScript links like: <script type="text/javascript" language="JavaScript1.2" src="./Resources/Menu2.js"></script>
    Is there any way to create one JavaScript file who will have all links to the JavaScript?
     
    ignas2526, Jul 18, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It depends what they're doing. Generally people prefer you throw all your tags with functions and stuff in the head since that's what the head is there for. When the browser reaches the script tag, it will parse the code in it before it moves onto other stuff (such as if you're declaring variables in it or making calls to functions). This is really the only time it'd matter where you place the code.

    If you're doing something like <script>document.write "hi";</script> then it's important where you put it because where ever you put it is where it will output "hi"
     
    zerxer, Jul 19, 2008 IP
  3. Daniel_S

    Daniel_S Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    And also if you want to include a script-file from another domain (e.g. Google Analytics), you should put your script-tags at the end of your page.
     
    Daniel_S, Jul 19, 2008 IP
  4. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Whether you put it in the BODY or HEAD makes no difference. However, the browser will follow the order of the source. Before calling any function, we should make sure that the function is declared.

    Eg:
    
    <html>
    <head>
    </head>
    <body>
    <script type="text/javascript">
    myFunc();
    function myFunc()
    {
    alert("Hey!");
    }
    </script>
    </body>
    </html>
    
    HTML:
    The above will generate an error because we have called the function when it doesn't exists at the instance of calling it.

    If you put in the HEAD of the document, the browser will register every function. Hence, any function can be called at any moment because all the functions have registered in the memory of the browser.

    Bottom line: No matter where you put the script, always declare the function before calling it.

    @Daniel S, that not always correct my dear.
    The reason we usually put analytics code at the bottom is because we do not want to keep the visitor waiting for the code to download and get implemented. Even if the code is not implemented, the page should work. If its put at the top of the page, then the browser will first load the code and implement it and later move on to loading the actual content of the page. You don't want to keep your visitors waiting.
     
    rohan_shenoy, Jul 19, 2008 IP
  5. Daniel_S

    Daniel_S Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's what I have meant, but described in detail. Thanks for the detailed explanation of my thoughts ;)
     
    Daniel_S, Jul 19, 2008 IP
  6. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks guys, i already found about what browser reads code from up to down, wan i putted javascript menu code in bottom of page all messed up because menu must be always on top, so if i scrolled wan forum based on javascript was loading the menu was always in center of page :D.
    My code collection starts from script who bookmarks, wan very advanced calculator, wan code for faq who shows and hides on press, wan two codes who holds values (what values what) used for creating tables, wan script to ban IPs, wan again script who holds tons of values, wan menu, wan menu core, and last one code who scrolls page title and status text. In total, there is 14 scripts, and size all of them are about 1mb, and planning to add more scripts.
     
    ignas2526, Jul 19, 2008 IP