Javascript hit counter for a website

I'm trying to code a hit counter for a website. As a preference, not required, I'm trying to use Javascript to do this. On the website's html file, I'm reading a number from a javascript file and outputting that. How can I make it "count" every time a hit occurs?

Sorry for such a vague question. Am I on the right track going about this? Any tips, code, or references will be much appreciated.




Using Javascript you can only count the hits from the current user, because you have to save the count in a cookie or a client side technology.

For a more accurate way you have to use a server-side language and maybe add a storage engine like a database to make it work.

I suggest you start here Php for beginners the link might be old but it covers all the basics you need to know to create your counter.


Javascript cannot change a file on a server or client computer without security issues. You may want to also look into loading another page as a blank image using PHP or another server side script language. That can then update the count on the file with no issues.


Most sites with a counter I see with a lot of traffic use a timed counter and use ajax to get the real number upon load.

The benefits of this is you do not have a static counter and you get the real number once you refresh but like it has been said before, with only javascript it is not possible


Javascript is a client side scripting language - it runs on client pc. The counter itself must be implemented at the server side, since you have to count http requests. The javascript code though may request the server to update the counter at the html page displayed at client's pc.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.