我们正在编写离线第一个应用程序基础知识的教程,并使用带磁带的JSDOM来测试我们的代码.在我们的代码中,我们更新DOM,以便通过将事件监听器附加到窗口并监听"在线"/"离线"事件,将文本节点从"在线"更改为"离线",反之亦然,并navigator.onLine初始化文本到在线/离线.像这样:
// get the online status element from the DOM
var onlineStatusDom = document.querySelector('.online-status');
// navigator.onLine will be true when online and false when offline. We update the text in the online status element in the dom to reflect the online status from navigator.onLine
if (navigator.onLine) {
onlineStatusDom.innerText = 'online';
} else {
onlineStatusDom.innerText = 'offline';
}
// we use the 'online' and 'offline' events to update the online/offline notification to the user
// in IE8 the offline/online …Run Code Online (Sandbox Code Playgroud)