By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,842 Members | 2,440 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,842 IT Pros & Developers. It's quick & easy.

Problem with link counter

P: 6
I have this code I would like to use as a link counter but it just will not store the number nor increment it. please help. I need this ASAP.



[HTML]
<!-- Start: JAVASCRIPT for Counter -->


<script type="text/javascript">
/* add 1 to the total */
function addItem(theCount){
if(parseFloat(document.getElementById(theCount).in nerHTML) > 0){
document.getElementById(theCount).innerHTML = parseFloat(document.getElementById(theCount).inner HTML) + 1;
}
}
/* removes 1 from total as long as total is above 0
function removeItem(theCount){
if(parseFloat(document.getElementById(theCount).in nerHTML) > 0){
document.getElementById(theCount).innerHTML = parseFloat(document.getElementById(theCount).inner HTML)-1;
}
} */

/* resets all items according the tagToCheck param to 0
function resetAllItems(tagToCheck){
var numElements = document.getElementsByTagName(theCount).length;
var i = 0;
while (i < numElements){
document.getElementsByTagName(theCount)[i].innerHTML = 0;
i++
}
} */

/* post all item counts to your ASP page */
function postItems(){
$count = 0
document.getElementById('MentalHealthManual').valu e = document.getElementById('MentalHealthManual').inne rHTML;
//document.getElementById('txtHam').value = document.getElementById('countHam').innerHTML;
//document.getElementById('txtCola').value = document.getElementById('countCola').innerHTML;
//document.getElementById('frmPostItems').submit();
}
</script>

<!-- END: JAVASCRIPT for Counter -->
<p><a href="http://vaww.st-louis.med.va.gov/training/MH_National_Manual_07_02_08_v2.pdf" onclick="addItem('MentalHealthManual'); return false;"><img src=../employee_news/images/BigNew.gif alt="click to download item" /></a>
<br /><span id="MentalHealthManual" onclick="removeItem('MentalHealthManual');" class="itemCount">0</span> Tuna sandwich</p>[/HTML]
Aug 5 '08 #1
Share this Question
Share on Google+
10 Replies


acoder
Expert Mod 15k+
P: 16,027
addItem can never increment because the initial value is 0 and you've got a check that the value is greater than 0. Remove the if-condition.

Secondly, removeItem is commented out.
Aug 5 '08 #2

P: 6
Yes I have many things commented out I just want to increment and for it to stay up. Just as a counter. But I have problems incrementing and making it hold its value. help!
Aug 5 '08 #3

P: 6
addItem can never increment because the initial value is 0 and you've got a check that the value is greater than 0. Remove the if-condition.

Secondly, removeItem is commented out.
I removed the if statement but the value goes back to 0. I want it to stay and increment. What can I do??
Aug 5 '08 #4

acoder
Expert Mod 15k+
P: 16,027
When you say you want it to increment, do you mean automatically by time (e.g. every second) or do you mean on clicking the link?

Also when you say it goes back to 0, do you mean when you leave the page and come back to it? or do you mean something else?
Aug 6 '08 #5

P: 6
When you say you want it to increment, do you mean automatically by time (e.g. every second) or do you mean on clicking the link?

Also when you say it goes back to 0, do you mean when you leave the page and come back to it? or do you mean something else?

I want the counter to increment everytime I click on the link. When I click on the pic. it increments to 1 and opens the page I want. But when I go back to the page it resets itself to 0. I want this to increment by 1 everytime I click it.
Aug 6 '08 #6

acoder
Expert Mod 15k+
P: 16,027
You'll need to use cookies to store the number. When you return back to the page, check for the cookie. If it's set, set the counter to the cookie value, otherwise leave it at 0.

To learn how to read, write and delete cookies, see this tutorial.
Aug 6 '08 #7

P: 6
You'll need to use cookies to store the number. When you return back to the page, check for the cookie. If it's set, set the counter to the cookie value, otherwise leave it at 0.

To learn how to read, write and delete cookies, see this tutorial.

[HTML]

<!-- Start: JAVASCRIPT for Counter -->
<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
}

// -->
</SCRIPT>
<!-- END: JAVASCRIPT ROUTINES For Counter -->
[/HTML]

I am using this code to cookies but it is another persons work all I want is to display the value where is the value???
Aug 6 '08 #8

acoder
Expert Mod 15k+
P: 16,027
Try using the code in the link and see the example code. To set the value, use setCookie() and to read it, getCookie(). Use something similar to the checkCookie() function onload.
Aug 6 '08 #9

P: 6
thanks tried it and it came out good.
Aug 7 '08 #10

acoder
Expert Mod 15k+
P: 16,027
Glad to hear it. Post again if you have any more questions.
Aug 7 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.