简体   繁体   中英

to show alert message when user closes particular page for first time

I have jquery which shows alert message when page loads for first time and not again but now i want jquery which shows alert message when page is closed for first time only Please can anyone help me what changes can be made in give jquery which is for page load?

<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>

That's really overcomplicating things?

To alert a message the first time any page loads, you'd do:

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

You can't really alert anything on beforeunload , but you can pop up a confirm dialog:

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

Support for older browsers can be added with the localStorage shim from MDN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM