Sunday, March 27, 2011

How does the location of a script tag in a page affect a javascript function that is defined in it?

I read that you should define your javascript functions in the <head> tag, but how does the location of the <script> (whether in the <head>, <body>, or any other tag) affect a javascript function.

Specifically, I would like to know how does it affect the scope of the function and where you can call it from?

From stackoverflow
  • If you have an inline script (outside functions) located before functions it may call, you may get an error because they may not be not available yet. Not saying it is always going to happen, just that it may depending on browser type or version.

  • It doesn't. Most programming framework scatter scripts all throughout the page. I've only rarely seen problems because of that (and only from older browsers).

    Simon_Weaver : hey! who's downvoting this without an explanation. perhaps someone who had lots of problems ??
  • One of the aspects of placement is performance. See this fine article within the YSlow discussion for why it's sometimes recommended you put them at the bottom of the document.

    As for issues of scope, the usual visibility rules for Javascript (vars defined inside or outside of functions, local, global, closures, etc.) are not affected so far as I know.

  • If your script refers to an ID on the page and the page has not been rendered (i.e. script is before HTML, or your script is executed with onload, rather then the DOM is ready) you can also get an error.

  • Javascript's scoping rules are similar to perl - you can call any function at the current or any higher scope level. The only restriction is that the function has to be defined at the time you call it. The position in the source is irrelevant - only the position in time matters.

    You should avoid putting scripts in the <head> if possible as it slows down page display (see the link Alan posted).

  • The normal rules of play still stand; don't use stuff before it's defined. :)

    Also, take note that the 'put everything at the bottom' advice isn't the only rule in the book - in some cases it may not be feasible and in other cases it may make more sense to put the script elsewhere.

    The main reason for putting a script at the bottom of a document is for performance, scripts, unlike other HTTP requests, do not load in parallel, meaning they'll slow down the loading of the rest of your page. Another reason for putting scripts at the bottom is so you don't have to use any 'DOM ready' functions. Since the script tag is below all elements the DOM will be ready for manipulation!

    EDIT: Read this: http://developer.yahoo.com/performance/rules.html#js_bottom

    Mark Rogers : adding at the bottom affects seo as well, I'm told
    Alan : Excellent point, m4bwav
  • Telling people to add <SCRIPT> only in the head sounds like a reasonable thing to do, but as others have said there are many reasons why this isn't recommended or even practical - mainly speed and the way that HTML pages are generated dynamically.

    This is what the HTML 4 spec says :

    The SCRIPT element places a script within a document. This element may appear any number of times in the HEAD or BODY of an HTML document.

    And some sample HTML. Doesn't it look pretty all formatted here :)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
         "http://www.w3.org/TR/html4/strict.dtd">
    <HTML>
    <HEAD>
    <TITLE>A document with SCRIPT</TITLE>
    <META http-equiv="Content-Script-Type" content="text/tcl">
    <SCRIPT type="text/vbscript" src="http://someplace.com/progs/vbcalc">
    </SCRIPT>
    </HEAD>
    <BODY>
    <SCRIPT type="text/javascript">
    ...some JavaScript...
    </SCRIPT>
    </BODY>
    </HTML>
    

    And something to look forward to in HTML 5 :

    New async attribute in <SCRIPT> :

    Note: There are ways [sic] a script can be executed:

    The async attribute is "true": The script will be executed asynchrously with the rest of the page, so the script will be executed while the page continues the parsing.

    The async attribute is "false", but the defer attribute is "true": The script will be executed when the page is finished with the parsing.

    Simon_Weaver : @m4bwav - i mainly wanted to make the additional point that hadn't been made that that it is OK to put SCRIPT in BODY for anyone who thought they shouldn't. in addition the Yahoo 'performance rules' article mentioned is well worth reading and covers the issue thoroughly

0 comments:

Post a Comment