简体   繁体   English

如何在用户点击链接后显示弹出窗口?

[英]How can I make a pop up stop appearing after a user clicks the link?

I've used JavaScript to set a cookie when the user clicks on the popup notice but after closing the browser completely, the cookie expires and the notice is shown again when navigating to the page. 当用户点击弹出通知时我使用JavaScript设置cookie,但在完全关闭浏览器后,cookie过期并在导航到页面时再次显示通知。

I want the user to have the option to essentially say "Don't show this again" . 我希望用户可以选择基本上说“不再显示”

1) Is there a better way to do this like saving the data to the db? 1)有没有更好的方法来保存数据到数据库?

2) Can I set the cookie so it doesn't expire when the window is closed? 2)我可以设置cookie,以便在窗口关闭时它不会过期吗?

Here is the code to set the cookie. 以下是设置cookie的代码。 If cookie isn't set to "visited" it calls another function to showLightBox() . 如果cookie未设置为“visited”,则调用另一个函数showLightBox()

$j(document).ready(function() { 

        $j(function() {
                    checkCookie();

                });

        function checkCookie() {
                var hasVisit = getCookie("hasVisited");

                if(hasVisit != "visited") {
                    showLightBox(); 
                }
        }

        function getCookie(c_name) {
            var c_value = document.cookie;
            var c_start = c_value.indexOf(" " + c_name + "=");

                   if (c_start == -1)
                   c_start = c_value.indexOf(c_name + "=");

           if (c_start == -1)
           c_value = null;          
           else
          {
              c_start = c_value.indexOf("=", c_start) + 1;
              var c_end = c_value.indexOf(";", c_start);
              if (c_end == -1)
              c_end = c_value.length;

                   c_value = unescape(c_value.substring(c_start,c_end));
          }
            return c_value;
        }

        function setCookie(cookieName, value) {

            document.cookie = cookieName + "=" + value;  
        }

Did you by any chance use a session cookie? 你有没有机会使用会话cookie? they expire when you close the browser. 它们会在您关闭浏览器时过期。

You can continue reading here : 你可以继续在这里阅读:

Why cookies dont expire after closing browser? 关闭浏览器后为什么cookie不会过期?

You can set the date when the cookie will be expired like this. 您可以像这样设置cookie过期的日期。

document.cookie="username=suman; expires=Thu, 18 Dec 2020 11:00:00 GMT";

Now this cookie will be deleted on 18th dec 2020. By default the cookie will be deleted when your browser is closed. 现在这个cookie将在2020年12月18日被删除。默认情况下,当您的浏览器关闭时,cookie将被删除。

Update:- In your case the cookie will set with the date on which it will be expired. 更新: -在您的情况下,cookie将设置它将过期的日期。

function setCookie(cookieName,value){
    document.cookie = cookieName+"="+value+"; expires=Thu, 18 Dec 2020 11:00:00 GMT";
}

You may put any date you want at date field. 您可以在日期字段中输入您想要的任何日期。

You are probably not setting an expiration for the cookie, so it defaults to a session cookie that expires on browser close. 您可能没有为cookie设置过期,因此它默认为在浏览器关闭时到期的会话cookie。

If you are already using jQuery I recommend using the wonderful jQuery Cookie plugin , which makes setting and retrieving cookies a total piece of cake. 如果您已经在使用jQuery,我建议使用精彩的jQuery Cookie插件 ,这使得设置和检索cookie成为一块蛋糕。 See the readme/documentation for details and enjoy only having to write a couple lines of code instead of 50. 有关详细信息,请参阅自述文件/文档,只需编写几行代码而不是50行代码。

Why don't you use localStorage in html5 for achieving this since it is persistent, Like when a user clicks on the link for the first time set the flag 为什么不在html5中使用localStorage来实现这一点,因为它是持久的,就像用户第一次点击链接时设置标志

localStorage.setItem('clicked', "yes");

Then again check if this value is set, 然后再次检查是否设置了此值,

   if(localStorage.getItem('clicked')!=null){
     //clicked
   }
   else
   {
    //not clicked
   }

If this value is set then show "Don't show this again" else don't ... 如果设置了此值,则显示“不要再显示”,否则不要......

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

相关问题 当用户单击菜单按钮时,如何弹出菜单? - How do I make a menu pop up when the user clicks the menu button? 如何在表单提交后弹出div - How can I make a div pop up after a form submits 我能否创建仅在单击几次后才能起作用的链接 - Can I make a link that only works after several clicks 如何生成警报/弹出式窗口,提示在用户单击浏览器之前避免单击浏览器的后退按钮 - How can I generate an alert/pop-up saying avoid clicking back button of browser before user clicks it 用户点击链接后我可以操纵DOM吗? - Can I manipulate the DOM after the user clicks a link? 我怎样才能让这个自动弹出? - How can I make this pop up automatic? 单击提交时,如何阻止出现 email 验证弹出窗口? - how do I stop a email validation pop-up from appearing when I click submit? 如何制作弹出式窗口,以创建其他弹出式窗口? - How can I make a pop up that creates other pop ups? 当用户在编辑后单击链接时,如何从HTML文本字段捕获更改事件? - How can I catch a change event from an HTML text field when a user clicks on a link after editing? 用户单击NetSuite中SO的“关闭订单”按钮后如何弹出确认对话框? - How to pop up a confirmation dialog after user clicks the “Close Order” button of SO in NetSuite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM