2012-09-22 80 views
1

您好,我正在研究在JavaScript中製作一個確認菜單,它將運行一組代碼,具體取決於您選擇是或否。JS中的確認彈出框

現在我希望它發生在window.onbeforeunload事件,但只有當個人按是的我希望剩下的代碼工作,如果他們按不,我希望window.onbeforeunload完全取消。我想知道它是否可能以及如何。這是我到目前爲止。我之所以要這樣做,是因爲當我運行腳本時,彈出窗口顯示出來,但在有人會選擇留下或離開之前。 click();功能啓動擦除信息。我想要.click();在返回時按下是且僅當他們按下是時啓動。

var validNavigation = false; 

function wireUpEvents() { 

var dont_confirm_leave = 0; 
var leave_message = document.getElementById("kioskform:broswerCloseSubmit"); 
var leave_safari = document.getElementById("kioskform:broswerCloseSafari"); 
     function goodbye(e) { 
     if (!validNavigation) { 
function disp_confirm() 
{ 
var leaveMessage=confirm("Are you sure you want to leave") 
if (leaveMessage==true) 
{   if (dont_confirm_leave!==1) { 
     if(!e) e = window.event; 
     //for IE 
     e.cancelBubble = true; 
     e.returnValue = leave_message.click(); 
     //e.stopPropagation works in Firefox. 
     if (e.stopPropagation) { 
      e.stopPropagation(); 
      e.preventDefault(); 
     } 
     //return works for Chrome and Safari 
     leave_safari.click(); 
     return ''; 

     //add the code to delete the kiosk information here. 
     // this is what is to be done. 
     } 
    } 

    else 
{ 
Alert("Returning to the page.") 
} 
    } 
    window.onbeforeunload=goodbye; 

    // Attach the event keypress to exclude the F5 refresh 
    jQuery('document').bind('keypress', function(e) { 
    if (e.keyCode == 116){ 
     validNavigation = true; 
    } 
    }); 

    // Attach the event click for all links in the page 
    jQuery("a").bind("click", function() { 
    validNavigation = true; 
    }); 

    // Attach the event submit for all forms in the page 
jQuery("form").bind("submit", function() { 
    validNavigation = true; 
    }); 

    // Attach the event click for all inputs in the page 
jQuery("input[type=submit]").bind("click", function() { 
    validNavigation = true; 
    }); 

} 

// Wire up the events as soon as the DOM tree is ready 
jQuery(document).ready(function() { 
    wireUpEvents(); 
}); 

回答

1

爲什麼不只是使用window.confirm

+0

我剛剛注意到,但它會與IE,FF,Chrome和Safari的工作?以及如何將其納入腳本? –

+1

刪除你的腳本,好像完全沒用 – hendry

+0

好了,然後呢?我只是想知道它是否仍然可以在onbeforeunload方法上工作。 –