Offline Apps & Drag-and-Drop
Cache manifests for offline use and the native Drag & Drop API.
HTML5 Offline Applications
HTML5 supports local caching of web app code and content, enabling use without an internet connection. A cache manifest lists what to store.
CACHE MANIFEST
# Version 1.0.2
CACHE:
index.html
styles/app.css
scripts/app.js
images/logo.png
NETWORK:
api/login.php
FALLBACK:
/app/ /offline.html
| Section | Meaning |
|---|---|
CACHE | Files listed here are downloaded once and served from the local cache thereafter. |
NETWORK | Files in this section always require a network connection — never cached. |
FALLBACK | Specifies a fallback page to serve when a network resource is unavailable. |
Modern practice replaces App Cache with Service Workers, which give finer control over offline caching.
HTML5 Drag and Drop API
<div id="item" draggable="true">Drag me!</div>
<div id="zone">Drop here</div>
const item = document.getElementById("item");
const zone = document.getElementById("zone");
// Events on the draggable element
item.addEventListener("dragstart", (e) => {
e.dataTransfer.setData("text/plain", e.target.id);
e.target.style.opacity = "0.5";
});
item.addEventListener("dragend", (e) => {
e.target.style.opacity = "1";
});
// Events on the drop target
zone.addEventListener("dragover", (e) => {
e.preventDefault(); // allow the drop
});
zone.addEventListener("drop", (e) => {
e.preventDefault();
const id = e.dataTransfer.getData("text/plain");
zone.appendChild(document.getElementById(id));
});
| Event | Fires when |
|---|---|
dragstart | Dragging begins |
drag | Continuously while dragging |
dragend | The drag operation ends |
dragenter | A dragged item enters a target |
dragover | Dragging over a target |
dragleave | The item leaves a target |
drop | The item is released on a target |
Activity — complete the code
Complete the code correctly to mark this lesson as complete automatically.
Complete the Drag and Drop handlers.
<div id="item" ="true">Drag me!</div>zone.addEventListener("", (e) => {e.preventDefault();});zone.addEventListener("drop", (e) => {const id = e..getData("text/plain");});