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