如何创建链接计数器?

时间:2019-05-27 06:11:37

标签: javascript php ajax

如何创建链接计数器并永久存储链接。 下面是链接计数器(W3Schools)的示例,但是此代码中的问题是它在不同设备中显示不同的计数。因此,我想对所有设备进行相同的计数。怎么做...请帮助。

<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter() {
  if (typeof(Storage) !== "undefined") {
    if (localStorage.clickcount) {
      localStorage.clickcount = Number(localStorage.clickcount)+1;
    } else {
      localStorage.clickcount = 1;
    }
    document.getElementById("result").innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s).";
  } else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
  }
}
</script>
</head>
<body>

<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>

</body>
</html>

0 个答案:

没有答案