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

Offline Apps & Drag-and-Drop

4 slides · use ←/→ or Space to navigate, F for fullscreen.

Reading view

← Back to the full Chapter 4 deck

Offline Apps & Drag-and-Drop
Fullscreen for presentation · Print / PDF for a handout
1 / 4
Lesson

Offline Apps & Drag-and-Drop

Cache manifests for offline use and the native Drag & Drop API.

Offline Apps & Drag-and-Drop

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.

Offline Apps & Drag-and-Drop

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
Offline Apps & Drag-and-Drop

Activity — complete the code

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");
});