javascript - 当用户第一次关闭特定页面时显示警报消息

标签 javascript jquery

我有jquery,它在页面第一次加载时显示警报消息,不再显示警报消息,但现在我想要jquery,它仅在页面第一次关闭时显示警报消息请任何人帮助我可以在给jquery中进行哪些更改用于页面加载?

<script type="text/javascript">

// First Time Visit Processing
// copyright 10th January 2006, Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the below code in this script (including this
// comment) is used without any alteration
function rC(nam)
 {
    var tC = document.cookie.split('; ');
     for (var i = tC.length - 1; i >= 0; i--)
      {
        var x = tC[i].split('=');
         if (nam == x[0])
          return unescape(x[1]);
       }
        return '~';
} 
function wC(nam,val)
{
    document.cookie = nam + '=' + escape(val);
} 
function lC(nam,pg) 
{
    var val = rC(nam);
    if (val.indexOf('~'+pg+'~') != -1)
        return false;
    val += pg + '~'; 
    wC(nam,val);
    return true;
} 
function firstTime(cN) 
{
    return lC('pWrD4jBo',cN);
} 
function thisPage() 
{
    var page = location.href.substring(location.href.lastIndexOf('\/')+1);
    pos = page.indexOf('.');
    if (pos > -1) 
    {
        page = page.substr(0,pos);
    } 
return page;
}

// example code to call it - you may modify this as required
function start() {
   if (firstTime(thisPage())) {
      // this code only runs for first visit
      alert('welcome');
   }
   // other code to run every time once page is loaded goes here
}
onload = start;

    </script>

最佳答案

这真的让事情变得过于复杂了吗?

要在第一次加载任何页面时发出提醒消息,您需要执行以下操作:

if ( ! localStorage.getItem(window.location) ) {
    localStorage.setItem(window.location, true);
    alert('Welcome');
}

您实际上无法在 beforeunload 上发出任何警报,但可以弹出一个确认对话框:

window.onbeforeunload = function(e) {
    if ( ! localStorage.getItem('unload_' + window.location) ) {
        localStorage.setItem('unload_' + window.location, true);
        return 'Dialog text here.';
    }
}    

可以使用 MDN 中的 localStorage shim 添加对旧版浏览器的支持.

关于javascript - 当用户第一次关闭特定页面时显示警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17992576/

相关文章:

javascript - html多文件输入-javascript访问和删除文件

javascript - if(数字)返回什么?

javascript - 在 AngularJS 中调用另一个 Controller

javascript - 我如何指向两个字段以在 javascript 中使用相同的值

jquery:从点击的div中获取值

javascript - 禁用提交按钮,直到选中一组动态创建的单选按钮中的一个

javascript - 使用 jQuery.when 时奇怪的变量初始化

JavaScript 范围 : How to create anonymous functions with different values?

javascript - Mobiscroll 日期选择器被 jQuery Mobile 模式隐藏

javascript - Jquery触发动画然后div HTML随悬停故障随机变化