解决问题的最简单方法是使用客户端cookie。如果你还没有饼干包装,我建议你
js-cookie
.
在html页面上:
<div id="promotion-container" hidden> //you want it hidden by default
//if it is shown by default there will be an annoying screen flicker when the pages loads
<script src="/path/to/js.cookie.js"></script>
在jQuery页面上:
$(document).ready( function() {
if (!Cookies.get('name1') || !Cookies.get('name2')) {
$('#promotion-container').show();
};
Cookies.set('name1', customerId, {expires:14} ); //must include expiration else the cookie will expire when the browser closes.
Cookies.set('name2', eventName, {expires:14} ); //you might need to make an eventId if eventName is too large
});
的第二个输入
Cookies.set
是
'value'
为了饼干。如果
'value' = null
,然后
Cookies.get('name') = null
.
这是假设你已经有办法
customerId
和
eventName
对于每个用户。此外,您可能需要根据在页面上创建customerId的时间修改Cookies的设置位置。
编辑:
有两种方法可以按您描述的方式运行查询,但这两种方法都不能按您希望的方式运行
除非
使用会话cookie。
(一)
你有
res.render
在查询的内部。
这将确保
div
从未显示给已单击它的用户,但会严重损害站点的性能。无论客户机是否具有customerId,每次呈现页面时都将运行查询。你的网站将是痛苦的缓慢与大量的流量。
2个)
使用ajax通过客户端js运行POST请求,并将customerId与db进行比较;如果找到结果,则删除div。
这将按您所希望的方式运行,并且不会影响性能,但没有什么能阻止客户使用
burp
拦截POST请求。他们可以将数据参数更改为他们想要的任何参数,并确保为他们加载div。
我看到的解决这些问题的唯一方法是在用户单击div和服务器上的会话cookie时验证用户。(对于用户验证,我使用
passport
和
express-session
).
我可以告诉你我如何设置这个,但要使它具体到你的需要,我需要知道更多关于你的网站是如何设置的。
我误解了为什么你需要隐藏div,事后看来使用客户端cookie是个糟糕的主意。