仅在第一次访问页面时显示 jQuery 弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32155036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:51:16  来源:igfitidea点击:

Show jQuery popup only at first visit of a page

jquery

提问by rudrainnovative

I am new to jQuery, and I have some needs on my website. I want to show the jQuery div popup at the first time only when the user comes. No need to show again and again.

我是jQuery新手,我的网站上有一些需求。我只想在用户来的时候第一次显示 jQuery div 弹出窗口。无需一次又一次地展示。

Still I am using this, but I don't know how to hide at the second time:

我仍然在使用这个,但我不知道如何在第二次隐藏:

var isshow=0;
$(document).ready(function() {
   if (isshow == 0) {
     $('#jPopup').show();
   }
   isshow = 1;
});

But the ishow variable initializes every time.

但是 ishow 变量每次都会初始化。

回答by Umesh Sehta

You can use localstorage. It is easy to understand and use.

您可以使用本地存储。它易于理解和使用。

$(document).ready(function() {
    var isshow = localStorage.getItem('isshow');
    if (isshow== null) {
        localStorage.setItem('isshow', 1);

        // Show popup here
        $('#jPopup').show();
    }
});

It will show you the popup at first visit of your site.

它会在您第一次访问您的网站时显示弹出窗口。

回答by sap

You may use SessionStorageor LocalStoragefor this as per your need.

您可以根据需要为此使用SessionStorageLocalStorage

If you need to do only for that session, use SessionStorage. If it should be stored permanently in the user's browser, use LocalStorage.

如果您只需要为该会话执行此操作,请使用 SessionStorage。如果它应该永久存储在用户的浏览器中,请使用 LocalStorage。

    $(document).ready(function(){
        if(sessionstorage && !sessionStorage.getItem('isshow')){
            $('#jPopup').show();
            sessionStorage.setItem('isshow', true);
        }
    });

回答by Pawan

You can use localStoragefor this purpose as below:

为此,您可以使用localStorage,如下所示:

$(document).ready(function(){
   var shown= localStorage.getItem('isshow');
    if(shown !="t"){
        $('#jPopup').show();
        localStorage.setItem('isshow', "t");
    }
});

回答by Manoj Kumar

You can set a cookie to store a value and check if it is not set then show popup:

您可以设置一个 cookie 来存储一个值并检查它是否未设置然后显示弹出窗口:

$(document).ready(function() {
    var isshow = $.cookie("isshow");
    if (isshow == null) {
        $.cookie("isshow", 1); // Store

        // Show popup here
        $('#jPopup').show();
    }
});

Or you can set localStorage. Here is a working example. jsFiddle

或者你可以设置localStorage。这是一个工作示例。js小提琴

$(document).ready(function() {
    if(localStorage.getItem('popState') != 'shown') {
        $("#popup").delay(2000).fadeIn();
        localStorage.setItem('popState','shown')
    }
});

回答by Tal

You need to hide it on load or do it in css (recommended)and then check localstorage to see if it is the first visit.

您需要在加载时隐藏它或在css中进行(推荐)然后检查localstorage以查看它是否是第一次访问。

$(document).ready(function() {
        $('#jPopup').hide(); //hide on load or in css, later check if its the first visit
        var isshow= localStorage.getItem('status');
        //check if it is the first visit
        if (isshow == null || isshow == '') {
            //set variable to 1
            localStorage.setItem('isshow', 1);
            $('#jPopup').show();
        }
        });