简体   繁体   English

按钮onclick并将cookie保存在Drupal 7上

[英]Button onclick and save cookie on Drupal 7

I have a Drupal 7 local website, that has an article and a popup on the first page that leads to that article. 我有一个Drupal 7本地网站,该网站上有一篇文章,并在首页上弹出了该文章。 In that article I also have a button and I want when the button is clicked by a user, the website to "remember" the user and not show the popup again. 在那篇文章中,我也有一个按钮,当用户单击按钮时,我希望网站“记住”该用户,而不再次显示弹出窗口。 I thought I could change the text of the button and store it as a cookie. 我以为可以更改按钮的文本并将其存储为cookie。 This is the code for the button: 这是按钮的代码:

<button id="test" value="test" onclick="Press()">Press Here</button>
<script>
function Press() {
    var test= document.getElementById("test");
    test.innerHTML="Thank you";
    document.cookie = "Inner"= + test.value +"; expires=Fri, 5 Aug 2016 01:00:00 UTC;";}
</script>

And this is the code of the external js file that handles the popup: 这是处理弹出窗口的外部js文件的代码:

      function openColorBox(){

if (document.cookie.length != 0){
   var buttonvaluearray = document.cookie.split("=");
   document.getElementById("test").innerHTML = buttonvaluearray[1];
}

if (test.innerHTML != "Thank you"){
        document.getElementById("popup").style.visibility = "visible";
        document.onclick= function(){
        document.getElementById("popup").style.visibility = "hidden";
    }
  }
}
    setTimeout(openColorBox, 5000);

But when I load the page, the popup shows as usual, but the text of the button changes immediately from "Press here" to "0; Drupal.toolbar.collapsed". 但是当我加载页面时,弹出窗口照常显示,但是按钮的文本立即从“ Press here”更改为“ 0; Drupal.toolbar.collapsed”。 I am a new web developer so I am sure something is wrong with the code I used either for the cookie creation/testing or the if statement of the external javascript file, but I can't spot the mistake(s). 我是一名新的Web开发人员,因此我确定我用于Cookie创建/测试或外部javascript文件的if语句的代码有问题,但是我无法发现错误。 Any suggestions or opinions? 有什么建议或意见吗?

Case closed! 结案! All I had to do is to use localStorage from JavaScript, at the first script, and test it afterwards at the external js file in order to create the popup! 我要做的就是在第一个脚本中使用JavaScript的localStorage,然后在外部js文件中对其进行测试,以创建弹出窗口!

Trick is here.. 特技在这里

Rather than storing value at client side , please store it in server side and run the check on each page. 请不要将值存储在客户端,而应将其存储在服务器端,并在每页上运行检查。

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

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