Loading counter.js _after_ </body>...?

Discussion in 'Feature Request' started by nickpelling, Apr 6, 2009.

Thread Status:
Not open for further replies.
  1. nickpelling

    nickpelling New Member

    Joined:
    Feb 3, 2009
    Messages:
    20
    Likes Received:
    0
    In a separate thread here, I was advised to load StatCounter's counter.js towards the end of my HTML: however, I (like most users) want to display the counter right at the start of the page.

    The easiest way to achieve this would seem to be to follow loading the counter.js script (near the end of the page) with a tiny bit of JavaScript trickery to copy the visitor count into an HTMLElement (say, a <span> or whatever) at the top of the page.

    But if you're going to that much trouble, why not load counter.js _after_ the <body>...</body> block, so that the HTML can load as fast as possible & the visitor count gets updated whenever it happens to arrive back from the StatCounter server? Think of it as an AJAX-style asynchronous load. :)

    Is this already possible? Or if not, may I request that a way is added to make it possible?

    Thanks, ....Nick Pelling....
     
  2. webado

    webado Moderator

    Joined:
    Apr 29, 2004
    Messages:
    28,168
    Likes Received:
    1
    It has to be inside the <body>..</body> of the page. Placing anything after the </body> tag will be invalid code.


    You can use css to style the div in which you want to show the counter so that it will appear at the top in the rendered page in the browser, while the markup is at the bottom in the source code, by using an absolute position and a z-index value for that div's ID. Up to you. But the code HAS to be inside the body or it won't work properly plus it will be invalid code when checked with the w3 validator.
     
  3. nickpelling

    nickpelling New Member

    Joined:
    Feb 3, 2009
    Messages:
    20
    Likes Received:
    0
    Javascript doesn't have to be in the body!

    If you look at pages emitted by the Wayback Machine (the Internet Archive), you'll see a lump of url-translating JavaScript appended after </body>. So, the claim that JavaScript can only work in the head or the body is false, sorry.

    Oh, and absolute positions are ghastly - I have a variable-width layout on my blog, which I don't really want to sacrifice just because of StatCounter.
     
  4. Arne

    Arne New Member

    Joined:
    Sep 25, 2004
    Messages:
    5,464
    Likes Received:
    0
    Some Javascript may work outside head and body, and perhaps SC code also work there but it's no guarantee it works properly. Anyway, everything on a page should be within the body tags as there what the displayed content on a page is. Some things that people put in the head and everything below the closing body and html tag is invalid, even if it seams to "work".

    You don't need to "sacrifice" anything of your current layout, just because you set one tiny div block in absolute position. That's what CSS is all about, you have more options to position things on the page with it, than you have with a table layout and pure html. You set the div with the counter within your current divs and the position is according to and within the parent divs left, right, top and bottom "lines", wherever they are on different widths and positions.
     
  5. howard

    howard New Member

    Joined:
    Jun 26, 2005
    Messages:
    636
    Likes Received:
    0
    Two good reasons to put the Statcounter code last in the code:

    1. so Statcounter can report accurately all activity based on the whole content of the page

    2. in case the Statcounter partition your project is on is slow or stalled, so it won't stall the loading of your page
     
Thread Status:
Not open for further replies.

Share This Page