尝试在JavaScript中使用SessionStorage

发布于 2025-02-04 01:40:22 字数 1870 浏览 1 评论 0原文

我试图每次会话只显示一次弹出窗口。为了实现这一点,我正在使用SessionStorage函数。但是,每当我重新加载网页时,弹出窗口都会显示。你能让我知道我在哪里犯错吗?

dialogue = new IMu.App.Dialogue();
dialogue.setHtmlMessage(IMu.string('dialogue-heading'));
dialogue.show({ showDetails: true });

window.sessionStorage.setItem('message','true');
var is_dialogue = window.sessionStorage.getItem('message');
if (is_dialogue != 'true')
{
    dialogue.show();
}

以下是显示功能

show: function(options, callback)
{
   if (typeof(options) == 'function')
   {
       callback = options;
       options = undefined;
   }
   var test = jQuery('body').children('div.imu-dialogue');
   jQuery('body').children('div.imu-dialogue').remove();
   var owner = self.owner = jQuery('body').child('div', 'imu-dialogue');

    var box = owner.child('div', 'box');

    var message = box.child('div', 'message');
    if (this.message)
    {
        var value = this.message.value;
        var method = this.message.type;
        message[method](value);
    }
            
    if (self.details)
    {
        var details = box.child('div', 'details');
        for (var i in self.details)
        {
             if (! self.details.hasOwnProperty(i))
             continue;

             var detail = details.child('div', 'detail');

             var value = self.details[i].value;
             var method = self.details[i].type || 'text';

             detail[method](value);
          }

           var show = box.child('div', 'show-details');
           if (! options || options.showDetails !== true)
           {
                show.text('Details');
                details.hide();
           }
           else
           {
                details.show();
                show.text('I agree');
           }

           show.on('click', function()
           {
                 self.owner.remove();
           });
}

I'm trying to show a popup window only once per session. In order to achieve that I'm using sessionStorage function. however, the popup is showing up whenever I reload the web page. Can you please let me know where I'm making the mistake.

dialogue = new IMu.App.Dialogue();
dialogue.setHtmlMessage(IMu.string('dialogue-heading'));
dialogue.show({ showDetails: true });

window.sessionStorage.setItem('message','true');
var is_dialogue = window.sessionStorage.getItem('message');
if (is_dialogue != 'true')
{
    dialogue.show();
}

Below is the show function

show: function(options, callback)
{
   if (typeof(options) == 'function')
   {
       callback = options;
       options = undefined;
   }
   var test = jQuery('body').children('div.imu-dialogue');
   jQuery('body').children('div.imu-dialogue').remove();
   var owner = self.owner = jQuery('body').child('div', 'imu-dialogue');

    var box = owner.child('div', 'box');

    var message = box.child('div', 'message');
    if (this.message)
    {
        var value = this.message.value;
        var method = this.message.type;
        message[method](value);
    }
            
    if (self.details)
    {
        var details = box.child('div', 'details');
        for (var i in self.details)
        {
             if (! self.details.hasOwnProperty(i))
             continue;

             var detail = details.child('div', 'detail');

             var value = self.details[i].value;
             var method = self.details[i].type || 'text';

             detail[method](value);
          }

           var show = box.child('div', 'show-details');
           if (! options || options.showDetails !== true)
           {
                show.text('Details');
                details.hide();
           }
           else
           {
                details.show();
                show.text('I agree');
           }

           show.on('click', function()
           {
                 self.owner.remove();
           });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2025-02-11 01:40:22

您的逻辑向后。您需要检查sessionstorage项目是否已设置为 first 。如果尚未设置,请显示对话框然后设置。

if (!sessionStorage.getItem('notified')) {
  let dialogue = new IMu.App.Dialogue();
  dialogue.setHtmlMessage(IMu.string('dialogue-heading'));
  dialogue.show({ showDetails: true });
  sessionStorage.setItem('notified', 'true');
}

无需检查存储属性的值。简单地测试其存在就足够了。

You've got your logic backwards. You need to check if the sessionStorage item has been set first. If it has not been set, show the dialog and then set it.

if (!sessionStorage.getItem('notified')) {
  let dialogue = new IMu.App.Dialogue();
  dialogue.setHtmlMessage(IMu.string('dialogue-heading'));
  dialogue.show({ showDetails: true });
  sessionStorage.setItem('notified', 'true');
}

There is no need to check the value of the stored property. Simply testing for it's existence is enough.

呆橘 2025-02-11 01:40:22

忘记dialogue.show();

如果在浏览器的新选项卡中尝试以下代码:

window.sessionStorage.setItem('message','true');
is_dialogue = window.sessionStorage.getItem('message');
is_dialogue != 'true' // false 

刷新页面并运行,

is_dialogue = window.sessionStorage.getItem('message');
is_dialogue != 'true' // false 

您可能会在代码whick whick whick whick whick section storage上存储在安装或创建的上?

Forget the dialogue.show();

If you try in a new tab of your browser the following code:

window.sessionStorage.setItem('message','true');
is_dialogue = window.sessionStorage.getItem('message');
is_dialogue != 'true' // false 

refresh the page and run

is_dialogue = window.sessionStorage.getItem('message');
is_dialogue != 'true' // false 

You may have something in your code whick clean session storage on mounted or on created ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文