First page Back Continue Last page Image
- <!DOCTYPE html>
- <html>
- <head>
- <title>How many times have you been here before?</title>
- <script src="script04.js"></script>
- </head>
- <body>
- <h2 id="pageHits"> </h2>
- </body>
- </html>
- window.addEventListener("load",initPage,false);
- function initPage() {
- var expireDate = new Date();
- expireDate.setMonth(expireDate.getMonth()+6);
- var hitCt = parseInt(cookieVal("pageHit"));
- hitCt++;
- document.cookie = "pageHit=" + hitCt + ";expires=" + expireDate.toGMTString();
- document.getElementById("pageHits").innerHTML = "You have visited this page " + hitCt + " times.";
- }
- function cookieVal(cookieName) {
- var thisCookie = document.cookie.split("; ");
- for (var i=0; i<thisCookie.length; i++) {
- if (cookieName == thisCookie[i].split("=")[0]) {
- return thisCookie[i].split("=")[1];
- }
- }
- return 0;
- }