StatCounter User Forum  
StatCounter Free web tracker and counter

Go Back   StatCounter User Forum > StatCounter.com > Feature Request

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 04-06-2009, 11:36 AM
nickpelling nickpelling is offline
Junior Member
 
Join Date: Feb 2009
Posts: 20
Question Loading counter.js _after_ </body>...?

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  
Old 04-06-2009, 02:49 PM
webado's Avatar
webado webado is offline
Moderator
 
Join Date: Apr 2004
Location: Montreal, Quebec, Canada
Posts: 27,577
Default

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.
__________________
Christina
>>Forum Moderator<<

Please do not PM me for support. The forum is here for that.
  #3  
Old 04-08-2009, 10:33 AM
nickpelling nickpelling is offline
Junior Member
 
Join Date: Feb 2009
Posts: 20
Exclamation 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  
Old 04-08-2009, 10:58 AM
Arne Arne is offline
Master Member
 
Join Date: Sep 2004
Location: Sweden
Posts: 5,464
Default

Quote:
Originally Posted by nickpelling View Post
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.
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".

Quote:
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.
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  
Old 04-09-2009, 08:05 PM
howard howard is offline
Senior Member
 
Join Date: Jun 2005
Posts: 636
Default

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
Closed Thread

Tags
asynchronous, body, counter.js, javascript

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:39 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.