Небольшое улучшение по варианту :
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
Из-за оценки короткого замыкания , getObject()
будет сразу return null
, если key
не находится в хранилище. Он также не будет генерировать исключение SyntaxError
, если value
- ""
(пустая строка, JSON.parse()
не может справиться с этим).
Вы можете использовать document.onstop
Происходит, когда пользователь прерывает загрузку документа. Если происходит событие onstop, событие onload не запускается в документе.
document.onstop = OnStopDocument;
function OnStopDocument () {
alert ("The loading of the document has been aborted.");
}
function OnLoadDocument () {
alert ("The document has been loaded.");
}
<body onload="OnLoadDocument ()">
Loading the document
</body>