Jump to content

Javascript Cookie Counter


ChidoriSoul

Recommended Posts

Is there a way in Javascript, that you can make a page with a counter determining who many times an IP has visted it, or technically, clicked a button for searching for something, and it would say"You have searched " " times"Just like one in ASP, except in JS

Link to comment
Share on other sites

Ofcourse it's possible. Just everytime the script is run read the current value of the cookie increase it and modify the cookie, i don't have the time to create the whole script for you but i found one u might find usefull

<script LANGUAGE="JavaScript"><!-- function nameDefined(ckie,nme){   var splitValues   var i   for (i=0;i<ckie.length;++i)   {      splitValues=ckie[i].split("=")      if (splitValues[0]==nme) return true   }   return false}function delBlanks(strng){   var result=""   var i   var chrn   for (i=0;i<strng.length;++i) {      chrn=strng.charAt(i)      if (chrn!=" ") result += chrn   }   return result}function getCookieValue(ckie,nme){   var splitValues   var i   for(i=0;i<ckie.length;++i) {      splitValues=ckie[i].split("=")      if(splitValues[0]==nme) return splitValues[1]   }   return ""}function insertCounter() { readCookie() displayCounter()} function displayCounter() { document.write('<H3 ALIGN="CENTER">') document.write("You've visited this page ") if(counter==1) document.write("the first time.") else document.write(counter+" times.") document.writeln('</H3>') }function readCookie() { var cookie=document.cookie counter=0 var chkdCookie=delBlanks(cookie)  //are on the client computer var nvpair=chkdCookie.split(";") if(nameDefined(nvpair,"pageCount")) counter=parseInt(getCookieValue(nvpair,"pageCount")) ++counter var futdate = new Date() var expdate = futdate.getTime() expdate += 3600000 * 24 *30  //expires in 1 hour futdate.setTime(expdate) var newCookie="pageCount="+counter newCookie += "; expires=" + futdate.toGMTString() window.document.cookie=newCookie}// -->insertCounter();</SCRIPT>

and remember: Google is you're friend

Link to comment
Share on other sites

I don't know exactly what u mean with different counters on different pages. But wouldn't it be easyer to make the counter server-side else it would be difficult to make a counter for every search.

Link to comment
Share on other sites

But then what is you're search page made of?

Link to comment
Share on other sites

<center>You did not find a Mew or Mewtwo<script LANGUAGE="JavaScript"><!--function nameDefined(ckie,nme){var splitValuesvar ifor (i=0;i<ckie.length;++i){splitValues=ckie[i].split("=")if (splitValues[0]==nme) return true}return false}function delBlanks(strng){var result=""var ivar chrnfor (i=0;i<strng.length;++i) {chrn=strng.charAt(i)if (chrn!=" ") result += chrn}return result}function getCookieValue(ckie,nme){var splitValuesvar ifor(i=0;i<ckie.length;++i) {splitValues=ckie[i].split("=")if(splitValues[0]==nme) return splitValues[1]}return ""}function insertCounter() {readCookie()displayCounter()}function displayCounter() {document.write('<H4 ALIGN="CENTER">')document.write("You've searched ")if(counter==1) document.write("the first time.")else document.write(counter+" times.")document.writeln('</H3>')}function readCookie() {var cookie=document.cookiecounter=0var chkdCookie=delBlanks(cookie) //are on the client computervar nvpair=chkdCookie.split(";")if(nameDefined(nvpair,"pageCount"))counter=parseInt(getCookieValue(nvpair,"pageCount"))++countervar futdate = new Date()var expdate = futdate.getTime()expdate += 3600000 * 24 *30 //expires in 1 hourfutdate.setTime(expdate)var newCookie="pageCount="+counternewCookie += "; expires=" + futdate.toGMTString()window.document.cookie=newCookie}// -->insertCounter();</SCRIPT><br/><br/><b>Try Again?</b><br/><script type="text/javascript">function random_catch(){var random_number=Math.floor(Math.random()*502);if (random_number >= 0 && random_number <= 500)	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=18";else if (random_number >= 0 + 1 && random_number <= 500)	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=16";else if (random_number >= 0 + 1 && random_number <= 500)	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=15";else if (random_number >= 0 + 1 && random_number <= 500)	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=13";else	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=17";return false;}</script><select id="pokemon_select" name="pokemon"><option value="Continue">Proceed</option></select><input type="submit" value="Search!" onclick="return random_catch();" /></center>

Here is the page, but when i use the counter code on a different page (Same code) it starts off where i have visited this page.Like i am clicking on this page and it says "You have searched 29485 times"I put the code on a different page that i have never visited before, and it says "You have searched 29485 times"

Link to comment
Share on other sites

Cookies apply to the entire domain, they get sent with all requests to the same domain. If you want to track it per page you need to put each page name in the cookie also, or create cookies based on the page name with the value being the number you're tracking.In general, if you want to track it per-page, then you need to keep track of the page name.

Link to comment
Share on other sites

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...