{"id":8871,"date":"2014-08-12T05:06:28","date_gmt":"2014-08-11T19:06:28","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=8871"},"modified":"2020-06-24T12:52:45","modified_gmt":"2020-06-24T02:52:45","slug":"htmljavascript-hit-counter-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/htmljavascript-hit-counter-primer-tutorial\/","title":{"rendered":"HTML\/Javascript Hit Counter Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/HitCounter\/local_session_hits.html\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"HTML\/Javascript Hit Counter Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/HitCounter\/Javascript_HitCounter.jpg\" title=\"HTML\/Javascript Hit Counter Primer Tutorial\"   \/><\/a><p class=\"wp-caption-text\">HTML\/Javascript Hit Counter Primer Tutorial<\/p><\/div>\n<p>We discussed web page hit counters previously when we had the <a target=_blank href='#pjhhcpt' title='PHP\/Javascript\/HTML Hit Counter Primer Tutorial'>PHP\/Javascript\/HTML Hit Counter Primer Tutorial<\/a> as shown below, up and going &#8230; 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 &#8230; but what if you want to ensure somebody doesn&#8217;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 <a target=_blank title='HTML5 Web Storage' href='http:\/\/www.tutorialspoint.com\/html5\/html5_web_storage.htm'>link<\/a> regarding the functionality &#8230; thanks.<\/p>\n<p>So the <i>&#8220;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&#8221;<\/i> and the <i>&#8220;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&#8217;s mailbox, on the client side for performance reasons&#8221;<\/i> &#8230; to quote the <a target=_blank title='HTML5 Web Storage' href='http:\/\/www.tutorialspoint.com\/html5\/html5_web_storage.htm'>link<\/a> above again.<\/p>\n<p>Here we have a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/HitCounter\/\">live run<\/a> that opens in a new tab, or this <a href=\"#\" onclick=\" location.href='http:\/\/www.rjmprogramming.com.au\/Javascript\/HitCounter\/'; \">live run<\/a> will happen on top of this webpage, and have a look at what happens to the different hit counters.<\/p>\n<p>Here is some HTML\/Javascript programming source code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Javascript\/HitCounter\/local_session_hits.html_GETME\" title=\"local_session_hits.html\">local_session_hits.html<\/a> &#8230; and we hope it helps you out with something you are working on.<\/p>\n<hr \/>\n<p id='pjhhcpt'>Previous relevant <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=3712' title='PHP\/Javascript\/HTML Hit Counter Primer Tutorial'>PHP\/Javascript\/HTML Hit Counter Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/HitCounter\/\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"PHP\/Javascript\/HTML Hit Counter Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/HitCounter\/HitCounter.jpg\" title=\"PHP\/Javascript\/HTML Hit Counter Primer Tutorial\"   \/><\/a><p class=\"wp-caption-text\">PHP\/Javascript\/HTML Hit Counter Primer Tutorial<\/p><\/div>\n<p>Here is a tutorial that shows a way to create a very rudimentary PHP <a target=_blank title='hit explained by Wikipedia' href='http:\/\/en.wikipedia.org\/wiki\/Hit_%28internet%29'>Hit<\/a> Counter for a particular webpage.   By &#8220;for a particular webpage&#8221; 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.<\/p>\n<p>Link to some downloadable PHP programming code &#8230; rename to <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/PHP\/HitCounter\/includeoncehc.php_GETME' title='Download me'>includeoncehc.php<\/a>.<\/p>\n<p>Link to some downloadable PHP programming code &#8230; rename to <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/PHP\/HitCounter\/useofhc.php_GETME' title='Download me'>useofhc.php<\/a>.<\/p>\n<p>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 <em>innerHTML<\/em> and\/or <em>value<\/em>.  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 <a target=_blank title='was hoping, and yes, it is true!' href='http:\/\/www.thefreedictionary.com\/prefixal'>prefixial<\/a> wording.<\/p>\n<p>You&#8217;ll see <em>&lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1.0&#8243;&gt;<\/em> in the code for useofhc.php within <em>&lt;head&gt;<\/em> and <em>&lt;\/head&gt;<\/em> which is code used to make websites user-friendly to mobile phones (and maybe have a look at some of the links of using <a target=_blank title='viewport meta tag ideas' href='https:\/\/www.google.com.au\/search?q=%22viewport+meta+tag%22+%22mobile+phone%22&#038;ie=utf-8&#038;oe=utf-8&#038;rls=org.mozilla:en-US:official&#038;client=firefox-a&#038;gws_rd=cr'>this<\/a>).  Another approach to mobile phone friendly code is to express width values as percentage values, and you&#8217;ll see that principle being applied in useofhc.php also.   You don&#8217;t have PHP?   You could use a Javascript Cookie approach?   (<a target=_blank title='Javascript Cookie Hit Counter' href='https:\/\/www.google.com.au\/search?q=%22Javascript+cookie+hit+counter%22&#038;ie=utf-8&#038;oe=utf-8&#038;rls=org.mozilla:en-US:official&#038;client=firefox-a&#038;gws_rd=cr'>here<\/a>? &#8230; or run tutorial, go View->Page Source with your web browser to get some starting ideas)<\/p>\n<p>A PHP topic of interest that comes into play regarding this tutorial is the use of <em>require_once()<\/em> and\/versus <em>include_once()<\/em> so take a look at <a target=_blank title='require_once' href='http:\/\/php.net\/manual\/en\/function.require-once.php'>require_once information<\/a> and <a target=_blank title='include_once' href='http:\/\/php.net\/manual\/en\/function.include-once.php'>include_once information<\/a>.  You can actually do this job with one PHP where you plugin the contents of includeoncehc.php in place of the <em>require_once()<\/em> call, and you may want to try this.   My apologies for the file naming confusion where includeoncehc.php is actually used with a <em>require_once()<\/em> call &#8230; but, c&#8217;est la vie.<\/p>\n<p>You might be interested in blinking text, and this is achieved with this tutorial, by the use of <em>&lt;blink&gt;<\/em> and <em>&lt;\/blink&gt;<\/em>.<\/p>\n<p>So enjoy this live run <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/HitCounter\/\" title='PHP\/Javascript\/HTML Hit Counter Primer Tutorial'>here<\/a>.<\/p>\n<p><strong><em>Did you know?<\/em><\/strong><\/p>\n<p>You may notice that the word Javascript does not appear within the code of <em>useofhc.php<\/em> nor <em>includeoncehc.php<\/em> 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 <em>&lt;script&gt; &lt;\/script&gt;<\/em> notation, as used in this tutorial, where the PHP writes its own &#8220;Javascript&#8221; (by implication), and is the default language of HTML element event code as well, so, just as you can have <br \/><em>&lt;a href=&#8221;<a target=_blank title='Two Ronnies' href='http:\/\/www.youtube.com\/watch?v=h-mX9T2qyIQ#zkWMcRlE1mQ'>http:\/\/www.youtube.com\/watch?v=h-mX9T2qyIQ#zkWMcRlE1mQ<\/a>&#8221; onclick=&#8221;alert(&#8216;FUNEX?&#8217;);'&#8221; title=&#8221;Two Ronnies Question&#8221;&gt;FUNEX?&lt;\/a&gt;<\/em> <br \/>you can just as well use syntax like <br \/><em>&lt;a href=&#8221;<a target=_blank title='Two Ronnies' href='http:\/\/www.youtube.com\/watch?v=h-mX9T2qyIQ#zkWMcRlE1mQ'>http:\/\/www.youtube.com\/watch?v=h-mX9T2qyIQ#zkWMcRlE1mQ<\/a>&#8221; onclick=&#8221;Javascript: alert(&#8216;S,VFX&#8217;);'&#8221; title=&#8221;Two Ronnies Answer&#8221;&gt;S,VFX&lt;\/a&gt; <\/em>\n<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d3712' onclick='var dv=document.getElementById(\"d3712\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=Google+Chart#content\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d3712' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d3712' onclick='var dv=document.getElementById(\"d3712\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=Google+Chart#content\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d3712' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d8871' onclick='var dv=document.getElementById(\"d8871\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=HTML5\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d8871' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We discussed web page hit counters previously when we had the PHP\/Javascript\/HTML Hit Counter Primer Tutorial as shown below, up and going &#8230; and lo and behold it is still up and going. It dealt with hit counters on a &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/htmljavascript-hit-counter-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[327,567,576,578,652,777,997,1319,1373],"class_list":["post-8871","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-did-you-know","tag-hit-counter","tag-html","tag-html5","tag-javascript","tag-meta-tag","tag-programming","tag-tutorial","tag-viewport"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/8871"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=8871"}],"version-history":[{"count":1,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/8871\/revisions"}],"predecessor-version":[{"id":49453,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/8871\/revisions\/49453"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=8871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=8871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=8871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}