在這裏,我發現了一個JavaScript按鈕點擊計數器,點擊一個按鈕並將這個數字保存在一個名爲web存儲的東西中,我不知道那是真的。JavaScript中的按鈕點擊計數器
我知道的一件事是,這個腳本只適用於一臺電腦,這意味着如果我點擊10次按鈕,那麼如果任何其他訪問者點擊該按鈕,它將不會顯示我之前點擊過的點擊次數。
現在我需要的是,不管怎樣,無論是使用javascript還是php,點擊次數都應該保存在我的服務器的文本文件中,以後當其他訪問者訪問HTML頁面時,他也應該得到相同的數字它出現在文本文件中。
這裏HTML
頁的代碼。
<!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>
以簡單的方式,
有一個HTML頁面上的一個按鈕。
如果訪客A點擊它5次,關閉頁面。
後來,B訪客訪問的頁面,他應該得到的5號第一,然後他點擊的時候,它應該得到自動計算並保存。
您需要服務器端代碼才能產生此效果。你在用什麼語言? PHP? ASP? – Ohgodwhy
我在HTML頁面使用php –
@Samon Souza你需要創建一個'ajax調用'來發送數據到服務器。然後服務器需要評估響應,然後打開文件,寫入文件,然後關閉文件。現在,每次「請求」頁面時,您都需要在該文件中包含文本,並將該文本用作計數器。 – Ohgodwhy