HTML/Javascript Hit Counter Primer Tutorial

HTML/Javascript Hit Counter Primer Tutorial

HTML/Javascript Hit Counter Primer Tutorial

We discussed web page hit counters previously when we had the PHP/Javascript/HTML Hit Counter Primer Tutorial as shown below, up and going … and lo and behold it is still up and going. It dealt with hit counters on a more general server side way of counting total hits on a web page over time, but today we are going to talk about HTML5 Javascript methods that can help out with session and local-web-browser-based hit counts. This may not immediately sound very interesting because the results go back to 1 when you close the web browser window … but what if you want to ensure somebody doesn’t pay twice with a credit card transaction on an eCommerce website. The Javascript functionality presented here can help out with this situation, and may save your bacon if it becomes known your site can so easily double charge a customer. The scenario I am talking about is the customer opening multiple windows to do with the one transaction. The use of the hit counters in an intelligent way can avoid any bad user usage of your webpage, and please read this link regarding the functionality … thanks.

So the “Session Storage is designed for scenarios where the user is carrying out a single transaction, but could be carrying out multiple transactions in different windows at the same time” and the “Local Storage is designed for storage that spans multiple windows, and lasts beyond the current session. In particular, Web applications may wish to store megabytes of user data, such as entire user-authored documents or a user’s mailbox, on the client side for performance reasons” … to quote the link above again.

Here we have a live run that opens in a new tab, or this live run will happen on top of this webpage, and have a look at what happens to the different hit counters.

Here is some HTML/Javascript programming source code you could call local_session_hits.html … and we hope it helps you out with something you are working on.


Previous relevant PHP/Javascript/HTML Hit Counter Primer Tutorial is shown below.

PHP/Javascript/HTML Hit Counter Primer Tutorial

PHP/Javascript/HTML Hit Counter Primer Tutorial

Here is a tutorial that shows a way to create a very rudimentary PHP Hit Counter for a particular webpage. By “for a particular webpage” mean that to get a meaningful hit count for a website you probably are best placed to use a statistics package from your web provider or use a tool that the web provider gives you.

Link to some downloadable PHP programming code … rename to includeoncehc.php.

Link to some downloadable PHP programming code … rename to useofhc.php.

For this tutorial there is some flexibility in regards to what type of HTML element you use to display your Hit Count statistics. The code handles HTML elements with either/both Javascript DOM concepts of innerHTML and/or value. There is flexibility regarding your wording around the 0 (zero) of the HTML h1 element used in useofhc.php too, as long as 0 (zero), or other numbers for that matter, do not appear in your reworked prefixial wording.

You’ll see <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> in the code for useofhc.php within <head> and </head> which is code used to make websites user-friendly to mobile phones (and maybe have a look at some of the links of using this). Another approach to mobile phone friendly code is to express width values as percentage values, and you’ll see that principle being applied in useofhc.php also. You don’t have PHP? You could use a Javascript Cookie approach? (here? … or run tutorial, go View->Page Source with your web browser to get some starting ideas)

A PHP topic of interest that comes into play regarding this tutorial is the use of require_once() and/versus include_once() so take a look at require_once information and include_once information. You can actually do this job with one PHP where you plugin the contents of includeoncehc.php in place of the require_once() call, and you may want to try this. My apologies for the file naming confusion where includeoncehc.php is actually used with a require_once() call … but, c’est la vie.

You might be interested in blinking text, and this is achieved with this tutorial, by the use of <blink> and </blink>.

So enjoy this live run here.

Did you know?

You may notice that the word Javascript does not appear within the code of useofhc.php nor includeoncehc.php as above, so you may wonder how it can be claimed to involve Javascript. The reason is that Javascript is the default language of the <script> </script> notation, as used in this tutorial, where the PHP writes its own “Javascript” (by implication), and is the default language of HTML element event code as well, so, just as you can have
<a href=”http://www.youtube.com/watch?v=h-mX9T2qyIQ#zkWMcRlE1mQ” onclick=”alert(‘FUNEX?’);'” title=”Two Ronnies Question”>FUNEX?</a>
you can just as well use syntax like
<a href=”http://www.youtube.com/watch?v=h-mX9T2qyIQ#zkWMcRlE1mQ” onclick=”Javascript: alert(‘S,VFX’);'” title=”Two Ronnies Answer”>S,VFX</a>

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *