0

Hi guys this is a little strange, could be an easy fix I don't know as i'm new to javascript/programming in general, basically at the moment i've currently got a website that has a simple counter function set to an image as can be seen here:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
</style>
<title></title>
<script type="text/javascript">
var count = 0;
function countClicks() {
 count = count + 1;
    document.getElementById("p2").innerHTML = count;
}
</script>

</head>
<body>
<table border="0">

<td>
<div id="p2" style="padding-left:15px; font-size:30px; font-family:Showcard        Gothic">0</div><br/>
<div>
<img onclick="javascript:countClicks();" src="C:\Users\DOMINATION\Desktop\button.jpg"     alt="alt text" />
</div>
</td>
</body>
</html>

Basically I want it so that whenever the site is refreshed the counter is the same as when it was set the last time it was changed, e.g if someone goes onto the site and puts the counter up to 4, when another user comes on to the site/the same person refreshes the page the counter will still be at 4. Is this possible?

Erwin
  • 4,757
  • 3
  • 31
  • 41
AlexSaunders
  • 3
  • 1
  • 3

3 Answers3

2

You have to distinguish between something you store server-side (seen by all users) and something stored client-side for every user separately.

To what regards client-side storage you can make use of the localStorage where you can memorize the value entered by the user and hence make it available offline. The next time the user opens/refreshes the page he'll again get the value he entered before. Other users obviously won't see that value as it is stored on the local computer of every user independently.
This is a good article: http://www.html5rocks.com/en/tutorials/offline/storage/

On the other side if you want to share something with multiple users, you have to store it centrally somewhere, accessible for all of them: a server.

Juri
  • 32,424
  • 20
  • 102
  • 136
0

There's no easy fix using only Javascript in the client (Javascript placed in the html code).

For what you describe to work, the clicks performed by one user need to be shared to other users via a server. So you need some server-side logic for this to work.

Strille
  • 5,741
  • 2
  • 26
  • 40
0

It's not possible if you only use JavaScript and store it client side you also have to store it server side.

In your situation it's best to make an ajax call to the server to update the count clicks of your button. In the stackoverflow question blow there is an similar situation:

jQuery - Increase the value of a counter when a button is clicked

Community
  • 1
  • 1
Erwin
  • 4,757
  • 3
  • 31
  • 41