【问题标题】:How to execute Javascript alert when page loads the first time only仅在页面第一次加载时如何执行 Javascript 警报
【发布时间】:2014-02-06 20:13:43
【问题描述】:

我正在尝试执行 javascript 警报,但仅在浏览器/计算机第一次查看该页面或类似内容时才会发出警报。
这将如何完成?我已经将 Javascript 编写成我认为类似的内容。

function alert() {
    alert(" Please view this is Firefox");
}
if (first time viewing this page) {
    alert();
}

非常感谢您的帮助

【问题讨论】:

  • 您对使用本地存储感兴趣吗?
  • 你在使用 asp.net 应用程序吗?
  • 您可能需要小心调用声明的函数alert,然后尝试在其中调用alert()

标签: javascript jquery html


【解决方案1】:

您可以为此使用JQuery Cookie Plugin

function showPopUp() {
    var cookie = $.cookie('the_cookie');
    if(!cookie){
        alert(" Please view this in Firefox");
        $.cookie('the_cookie', 'the_value');
    }
}
showPopUp();

【讨论】:

  • 你不觉得 alert 是自定义函数的坏名字吗?
  • @Dharmang 你是对的。我已经更改了函数名称。
【解决方案2】:

您可以使用localStoragecookies

这是localStorage 的示例:

var visited = localStorage.getItem('visited');
if (!visited) {
  alert("Please view this is Firefox");
  localStorage.setItem('visited', true);
}

【讨论】:

    【解决方案3】:

    不要使用Cookie,它将在您每次发出请求时发送到服务器。您可以改用Local Storage,例如:

    function load() {
      var isFired = localStorage.getItem('checkFired');
    
      if (isFired != '1'){
          alert(" Please view this is Firefox");
          localStorage.setItem('checkFired', '1');
      }
    }
    load();
    

    【讨论】:

      【解决方案4】:

      看到这个链接你会知道example是基于cookies的......一旦你输入了你的值,如果你刷新它,它会显示这个值..

      【讨论】:

        【解决方案5】:

        设置 cookie

        document.cookie="first=first";
        

        使用 JavaScript 读取 Cookie

        var x = document.cookie;
        

        示例:

        function getCookie(cname)
        {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) 
          {
          var c = ca[i].trim();
          if (c.indexOf(name)==0) return c.substring(name.length,c.length);
          }
        return "";
        }
        
        function checkCookie()
        {
          var first=getCookie("first");
          if(first == "first")
          {
            alert(" Please view this is Firefox");
          }else{
            document.cookie="first=first";
          }
        }
        

        Cookie in JS

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多