简体   繁体   中英

How can I make a localstorage value in JavaScript into a serverstorage value?

I'm trying to create a project which involves users being able to vote, and taking or adding to a global bank of points.

This is my very basic prototype, but I need to find a way to have the localstorage value be global, and the same for every user, using serverstorage.

The code:

<!DOCTYPE HTML>
<html>

<head>
  <title>Javascript variable testing</title>

<script type="text/javascript">
    var clicks;

    function onClick() {
      clicks = +clicks + 1;
      document.getElementById("clicks").innerHTML = clicks;
      localStorage.setItem('clicks', clicks); // set the value to localStorage
    };

    window.onload = function() {
      clicks = localStorage.getItem('clicks') || 500000; // get the value from localStorage
      document.getElementById("clicks").innerHTML = clicks;
    };
</script>
</head>
<body>
  <button type="button" onclick="onClick()">Click this bit</button>
  <p>Clicks: <a id="clicks">500000</a>
  </p>
</body>

</html>

localStorage is meant for storing data in the User's Browser . You might need to store data into your Server for your requirement. You can use AJAX for the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM