So supposedly starting at Firefox > 4,綁定窗口jQuery對象beforeunload
不再工作。將AJAX發送到服務器之前undeunload
我想要做的是提交一個AJAX帖子來刪除我的服務器的memcache數據。
當我刷新只打開的選項卡,我可以看到beforeunload
事件被稱爲在Firefox和用下面的代碼由鍍鉻的消息的console.log,「火狐/ NON-火狐刪除」就是明證。問題是,我從來沒有看到console.log消息「memcache delete」,表明我的服務器從未看到$.ajax
請求。
我意識到做瀏覽器嗅探並且if和else語句中包含的內容沒有區別。我只是展示了我在Firefox中嘗試失敗的代碼。
任何人有任何想法?
$(window).bind('beforeunload', function(){
if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
console.log('firefox delete');
memcacheDelete();
return null;
}
else {
console.log('NON-firefox delete');
memcacheDelete();
return null;
}
});
function memcacheDelete() {
$.ajax({
url: "/memcache/delete",
type: "post",
data:{},
success:function(){
console.log('memcache deleted');
}//success
}); //ajax
}
是...我明白了非同步調用此路是我想... + 1 – cliffbarnes 2013-02-18 05:27:31
@Jashwant,爲explanation.Though感謝,我有證據表明,在關閉/重新打開瀏覽器後使用Chrome時,memcache刪除成功,但它仍然是**不是用firefox刪除。這裏似乎還有其他的東西......將'$ .ajax'轉換爲同步,'async:false',有幫助嗎? – 2013-02-18 05:33:47
它看起來像將[$ .ajax'選項'async'更改爲'false'](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings)可以糾正問題。有沒有什麼理由不想這樣做? – 2013-02-18 05:41:24