Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14
Chapter 4

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
SectionMeaning
CACHEFiles listed here are downloaded once and served from the local cache thereafter.
NETWORKFiles in this section always require a network connection — never cached.
FALLBACKSpecifies 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));
});
EventFires when
dragstartDragging begins
dragContinuously while dragging
dragendThe drag operation ends
dragenterA dragged item enters a target
dragoverDragging over a target
dragleaveThe item leaves a target
dropThe 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");
});