简体   繁体   English

如何永久保存链接点击?

[英]How do i store my link clicks permanently?

This is my code: 这是我的代码:

 <script type="text/javascript"> var clicks = 0; function onClick() { clicks += 1; document.getElementById("clicks").innerHTML = clicks; }; </script> <center> <button type="button" style="height: 40px; width: 200px" onClick="onClick()"> <a href="url">DOWNLOAD</a> </button> <p>Downloaded: <a id="clicks">0</a></p> </center> 

Here is the sample of what you want using SessionStorage. 这是您想要使用SessionStorage的示例。 The click counter will persist even on refresh of page. 点击计数器即使在刷新页面时也将持续存在。

Also, on every click you can store it on server. 另外,每次单击都可以将其存储在服务器上。

 <!DOCTYPE html> <html> <head> <script> function clickCounter() { if(typeof(Storage) !== "undefined") { if (sessionStorage.clickcount) { sessionStorage.clickcount = Number(sessionStorage.clickcount)+1; } else { sessionStorage.clickcount = 1; } document.getElementById("result").innerHTML = "You have clicked the button " + sessionStorage.clickcount + " time(s) in this session."; } 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 is reset.</p> </body> </html> 

You can also use localStorage for this case. 您也可以在这种情况下使用localStorage。 Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance. 本地存储更安全,可以在不影响网站性能的情况下在本地存储大量数据。

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server. 与cookie不同,存储限制更大(至少5MB),信息永远不会传输到服务器。

Here is an example 这是一个例子

 <!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> 

如果您想将其数据保存在浏览器中,请阅读有关cookie本地存储的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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