|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="UTF-8"> |
| 5 | +<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | +<title>Chapter 10: HTML APIs</title> |
| 7 | +<link rel="stylesheet" href="style.css"> |
| 8 | +<style> |
| 9 | + /* Drag & Drop Styles */ |
| 10 | + #div1 { |
| 11 | + width: 150px; |
| 12 | + height: 50px; |
| 13 | + background: #c0f0c0; |
| 14 | + text-align: center; |
| 15 | + line-height: 50px; |
| 16 | + margin-bottom: 10px; |
| 17 | + cursor: grab; |
| 18 | + } |
| 19 | + |
| 20 | + #div2 { |
| 21 | + width: 200px; |
| 22 | + height: 60px; |
| 23 | + border: 1px solid #000; |
| 24 | + text-align: center; |
| 25 | + line-height: 60px; |
| 26 | + margin-top: 10px; |
| 27 | + } |
| 28 | + |
| 29 | + /* Buttons spacing */ |
| 30 | + .tag-demo button { |
| 31 | + margin: 5px 5px 5px 0; |
| 32 | + } |
| 33 | + |
| 34 | + /* Worker output styling */ |
| 35 | + #worker-result { |
| 36 | + font-weight: bold; |
| 37 | + margin-left: 10px; |
| 38 | + } |
| 39 | +</style> |
| 40 | +</head> |
| 41 | +<body> |
| 42 | + |
| 43 | +<h2 class="chapter-header">Chapter 10: HTML APIs</h2> |
| 44 | + |
| 45 | +<!-- Drag and Drop --> |
| 46 | +<div class="tag-section"> |
| 47 | +<h3>Drag and Drop</h3> |
| 48 | +<div class="tag-demo"> |
| 49 | +<div class="head-demo"> |
| 50 | +<h5>Draggable Div</h5> |
| 51 | +<div id="div1" draggable="true">Drag me</div> |
| 52 | +<div id="div2">Drop here</div> |
| 53 | +<script> |
| 54 | +const draggable = document.getElementById("div1"); |
| 55 | +const dropzone = document.getElementById("div2"); |
| 56 | + |
| 57 | +draggable.addEventListener("dragstart", (ev) => { |
| 58 | + ev.dataTransfer.setData("text", ev.target.id); |
| 59 | +}); |
| 60 | + |
| 61 | +dropzone.addEventListener("dragover", (ev) => { |
| 62 | + ev.preventDefault(); |
| 63 | +}); |
| 64 | + |
| 65 | +dropzone.addEventListener("drop", (ev) => { |
| 66 | + ev.preventDefault(); |
| 67 | + const data = ev.dataTransfer.getData("text"); |
| 68 | + const draggedEl = document.getElementById(data); |
| 69 | + draggedEl.style.display = "none"; // hide after drop |
| 70 | + dropzone.textContent = "Dropped!"; |
| 71 | +}); |
| 72 | +</script> |
| 73 | +</div> |
| 74 | +</div> |
| 75 | +</div> |
| 76 | + |
| 77 | +<!-- Geolocation --> |
| 78 | +<div class="tag-section"> |
| 79 | +<h3>Geolocation</h3> |
| 80 | +<div class="tag-demo"> |
| 81 | +<div class="head-demo"> |
| 82 | +<h5>Get User Coordinates</h5> |
| 83 | +<button onclick="getLocation()">Get Location</button> |
| 84 | +<p id="geo-out"></p> |
| 85 | +<script> |
| 86 | +function getLocation() { |
| 87 | + const x = document.getElementById("geo-out"); |
| 88 | + if (navigator.geolocation) { |
| 89 | + navigator.geolocation.getCurrentPosition(function(position){ |
| 90 | + x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; |
| 91 | + }, function(){ |
| 92 | + x.innerHTML = "Unable to retrieve location."; |
| 93 | + }); |
| 94 | + } else { |
| 95 | + x.innerHTML = "Geolocation not supported"; |
| 96 | + } |
| 97 | +} |
| 98 | +</script> |
| 99 | +</div> |
| 100 | +</div> |
| 101 | +</div> |
| 102 | + |
| 103 | +<!-- Web Storage --> |
| 104 | +<div class="tag-section"> |
| 105 | +<h3>Web Storage</h3> |
| 106 | +<div class="tag-demo"> |
| 107 | +<div class="head-demo"> |
| 108 | +<h5>Local Storage Example</h5> |
| 109 | +<input type="text" id="name-local" placeholder="Enter name"/> |
| 110 | +<button onclick="setLocal()">Set Local Storage</button> |
| 111 | +<button onclick="getLocal()">Get Local Storage</button> |
| 112 | +<script> |
| 113 | +function setLocal() { |
| 114 | + localStorage.myName = document.getElementById('name-local').value; |
| 115 | +} |
| 116 | +function getLocal() { |
| 117 | + alert(localStorage.myName || "No value stored"); |
| 118 | +} |
| 119 | +</script> |
| 120 | + |
| 121 | +<h5>Session Storage Example</h5> |
| 122 | +<input type="text" id="name-session" placeholder="Enter name"/> |
| 123 | +<button onclick="setSession()">Set Session Storage</button> |
| 124 | +<button onclick="getSession()">Get Session Storage</button> |
| 125 | +<script> |
| 126 | +function setSession() { |
| 127 | + sessionStorage.myName = document.getElementById('name-session').value; |
| 128 | +} |
| 129 | +function getSession() { |
| 130 | + alert(sessionStorage.myName || "No value stored"); |
| 131 | +} |
| 132 | +</script> |
| 133 | +</div> |
| 134 | +</div> |
| 135 | +</div> |
| 136 | + |
| 137 | +<!-- Web Workers --> |
| 138 | +<div class="tag-section"> |
| 139 | +<h3>Web Workers</h3> |
| 140 | +<div class="tag-demo"> |
| 141 | +<div class="head-demo"> |
| 142 | +<h5>Web Worker Counter</h5> |
| 143 | +<p>Count numbers: <output id="worker-result">0</output></p> |
| 144 | +<button onclick="startWorker()">Start Worker</button> |
| 145 | +<button onclick="stopWorker()">Stop Worker</button> |
| 146 | +<script> |
| 147 | +let w; |
| 148 | + |
| 149 | +function startWorker() { |
| 150 | + if (typeof Worker !== "undefined") { |
| 151 | + if (typeof w === "undefined") { |
| 152 | + const workerCode = ` |
| 153 | + let counter = 0; |
| 154 | + const interval = setInterval(() => { |
| 155 | + counter++; |
| 156 | + postMessage(counter); |
| 157 | + }, 1000); |
| 158 | + onmessage = function(event) { |
| 159 | + if (event.data === 'stop') { |
| 160 | + clearInterval(interval); |
| 161 | + close(); |
| 162 | + } |
| 163 | + }; |
| 164 | + `; |
| 165 | + const blob = new Blob([workerCode], { type: "application/javascript" }); |
| 166 | + const blobURL = URL.createObjectURL(blob); |
| 167 | + w = new Worker(blobURL); |
| 168 | + } |
| 169 | + w.onmessage = function(event) { |
| 170 | + document.getElementById('worker-result').textContent = event.data; |
| 171 | + }; |
| 172 | + } else { |
| 173 | + alert("Web Workers not supported"); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +function stopWorker() { |
| 178 | + if (w) { |
| 179 | + w.terminate(); |
| 180 | + w = undefined; |
| 181 | + } |
| 182 | +} |
| 183 | +</script> |
| 184 | +</div> |
| 185 | +</div> |
| 186 | +</div> |
| 187 | + |
| 188 | +</body> |
| 189 | +</html> |
0 commit comments