0

我有 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>
4

1 回答 1

4

这真的太复杂了吗?

要在第一次加载任何页面时提醒消息,您可以:

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 添加对旧浏览器的支持。

于 2013-08-01T11:32:51.617 回答