不要在使用JS第二次单击时弹出表单

not to pop up a form on second click using js

本文关键字:单击 表单 第二次 JS      更新时间:2023-09-26

我的网站上有一个弹出表单,只要有人点击链接就会弹出。 但我想让它不应该为同一用户第二次弹出。

怎么做,因为我没有用户管理系统。

更好的解决方案,在Jquery中使用one

<a id="popup" >link1</a><br>
$('#popup').one('click',function(){
 alert('open your popup here');
 });

这是你的小提琴

您需要为此使用 cookie。首次点击时,在用户首次点击时生成 Cookie,并在用户点击时检查它是否可用。

因此,作为一种解决方案,您可以使用cookie。下面是示例。

此支持功能将帮助您稍后使用 JS 获取 cookie

function getCookie(name) {
   var matches = document.cookie.match(new RegExp("(?:^|; )" + name.replace(/(['.$?*|{}'(')'[']'''/'+^])/g,'''$1')+"=([^;]*)"));
   var x = matches ? decodeURIComponent(matches[1]) : undefined;
   return x;
}

并且比在用户点击处理程序中为这样的浏览器设置 cookie

$(document).on('click','#someDivHere',function() {
   if (getCookie('addShowed') === undefined) {
      var date = new Date( new Date().getTime() - 2*24*60*60*1000 );
      document.cookie="addShowed='true'; path=/; expires="+date.toUTCString();
   }
});