2014-12-04 80 views
0

我想在用戶關閉瀏覽器時執行一些代碼。我找到了一些代碼並嘗試它,但它不起作用。我注意到它在jsfiddle中點擊Run鏈接時正在工作。我不知道爲什麼。beforeunload before not working when關閉標籤

$(function(){ 
$(window).on('beforeunload', function(){ 
    alert(0)  
}) 
}) 

Fiddle

+0

可能重複[Crossbrowser onbeforeunload?](http://stackoverflow.com/questions/7389554/crossbrowser-onbeforeunload) – 2014-12-04 15:32:43

+0

爲我工作,但你應該嘗試出小提琴,似乎被阻止。 – Hacketo 2014-12-04 15:34:57

+0

關於使用純[javascript](https://developer.mozilla.org/en-US/docs/WindowEventHandlers.onbeforeunload)怎麼辦? – empiric 2014-12-04 15:38:53

回答

0

在觸發beforeunload事件生成默認警報

$(function(){ 
$(window).on('beforeunload', function(){ 
return 'message'; 
}); 
}); 

可以重寫信息等上面的代碼示例