2013-08-01 1 views
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

정말 과장 되나요?

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을 사용하여 이전 브라우저를 지원할 수 있습니다.

+0

문제를 해결해 주셔서 감사합니다. – chithon

관련 문제