在我的應用程序中,我使用sessionStorage
來保存客戶端的數據。我的應用程序工作正常,但我想實現處理清除瀏覽器窗口崩潰時sessionStorage
。 如何在窗口崩潰時清除sessionStorage
。瀏覽器崩潰時清除sessionStorage
1
A
回答
0
我已經通過實施下面的代碼到我的index.html文件來達到的這樣:
window.addEventListener('load', function() {
sessionStorage.setItem('good_exit', 'pending');
setInterval(function() {
sessionStorage.setItem('time_before_crash', new Date().toString());
}, 1000);
});
window.addEventListener('beforeunload', function() {
sessionStorage.setItem('good_exit', 'true');
});
if(sessionStorage.getItem('good_exit') &&
sessionStorage.getItem('good_exit') !== 'true') {
/*
insert crash logging code here
*/
alert('Hey, welcome back from your crash, looks like you crashed on: ' + sessionStorage.getItem('time_before_crash'));
}
1
不知道爲什麼瀏覽器不會刪除所有sessionStogare 在重新啓動因爲這顯然是一個新的瀏覽實例...
但你可以在你的應用程序做什麼是始終首先明確使用
sessionStorage.clear(); // on application restart clear any eventual residues
// Your program logic here
這裏任何最終殘留物是一個基本的測試例子:
// 1. let's clear it
sessionStorage.clear();
// 2. Let's try to store a value
document.querySelector("button").addEventListener("click", function(){
sessionStorage.test = "TEST";
document.body.textContent = sessionStorage.test;
});
// 3. let's try to crash Chrome (copy this into a tab addressbar)
// chrome://inducebrowsercrashforrealz
// 4. on browser RESTORE we should see the button, not the stored value
if(sessionStorage.test) document.body.textContent = sessionStorage.test;
<button>CLICK TO STORE</button>
相關問題
- 1. jQuery崩潰IE瀏覽器
- 2. 角httpBackend崩潰瀏覽器
- 3. Angularfire在保存時崩潰瀏覽器
- 4. Nightwatch清除瀏覽器
- 5. ExternalInterface調用崩潰的瀏覽器
- 6. 網頁崩潰Android瀏覽器
- 7. 循環動畫崩潰瀏覽器?
- 8. AJAX崩潰瀏覽器(200MB響應)
- 9. 多個標記崩潰瀏覽器
- 10. 簡單頁面崩潰瀏覽器
- 11. 舊的JavaScript函數崩潰瀏覽器
- 12. 有限循環崩潰瀏覽器
- 13. 太多的JSON崩潰的瀏覽器:(
- 14. JavaScript的崩潰iPad的瀏覽器
- 15. While循環崩潰瀏覽器
- 16. GWT混淆崩潰iPad Safari瀏覽器
- 17. encodeURI文件下載 - 崩潰瀏覽器
- 18. eventsource正在崩潰我的瀏覽器
- 19. text-rendering:optimizeLegibility崩潰Android股票瀏覽器
- 20. jQuery .slideDown();崩潰的Android瀏覽器
- 21. IE8和CSS @ font-face崩潰瀏覽器
- 22. 快速location.replace導致瀏覽器崩潰
- 23. 如何調試SWF瀏覽器崩潰
- 24. 當瀏覽器清除緩存時清除HTML5 Web存儲?
- 25. 清除QTreeWidget時應用程序崩潰
- 26. FlashDevelop - FD附加到Flash 11調試器時瀏覽器崩潰
- 27. MVC3瀏覽器錯誤...'sessionStorage'未定義
- 28. 量角器測試:browser.get清除sessionStorage
- 29. 如何清除瀏覽器上的sessionstorage刷新,但是這個不應該清除點擊瀏覽器後退按鈕
- 30. 清除html5 sessionStorage跨http/https
有趣......當瀏覽器崩潰 - 它保留的sessionStorage重新啓動時? –
是的,它確實保留了sessionStorage –