-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path48.1_web_apis_2.js
103 lines (96 loc) · 3.11 KB
/
48.1_web_apis_2.js
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
/**
* ========================================================
* Web APIs - Part 2
* ========================================================
*/
/**
* ========================================================
* 1. WebSocket API
* ========================================================
* WebSocket provides full-duplex communication channels over a single, long-lived connection.
*/
const ws = new WebSocket("ws://example.com/socketserver");
ws.onopen = () => {
ws.send("Hello Server!");
};
ws.onmessage = (event) => {
console.log(`Server says: ${event.data}`);
};
ws.onclose = () => {
console.log("Connection closed");
};
/**
* ========================================================
* 2. DOM API
* ========================================================
* The DOM API allows you to programmatically interact with HTML and XML documents.
*/
// Adding a new element
const newElement = document.createElement("div");
newElement.textContent = "Hello, World!";
document.body.appendChild(newElement);
/**
* ========================================================
* 3. Drag and Drop API
* ========================================================
* This API allows you to make elements draggable and to capture drop events.
*/
document.addEventListener("dragstart", (event) => {
event.dataTransfer.setData("text/plain", event.target.id);
});
document.addEventListener("drop", (event) => {
event.preventDefault();
const data = event.dataTransfer.getData("text");
const target = document.getElementById(data);
event.target.appendChild(target);
});
/**
* ========================================================
* Nuances and Advanced Techniques
* ========================================================
*/
/**
* 1. WebSocket Reconnection
* --------------------------
* In real-world scenarios, WebSocket connections can drop. A common practice is to implement
* reconnection logic.
*/
let ws;
function connect() {
ws = new WebSocket("ws://example.com/socketserver");
ws.onopen = () => {
ws.send("Hello Server!");
};
ws.onmessage = (event) => {
console.log(`Server says: ${event.data}`);
};
ws.onclose = () => {
console.log("Connection closed. Reconnecting...");
setTimeout(connect, 1000);
};
}
connect();
/**
* 2. Using DOM API with Fragments
* --------------------------------
* Document fragments let you create a subtree of elements and insert them into the DOM
* as a single operation.
*/
const fragment = document.createDocumentFragment();
const elem1 = document.createElement("p");
elem1.textContent = "Paragraph 1";
const elem2 = document.createElement("p");
elem2.textContent = "Paragraph 2";
fragment.appendChild(elem1);
fragment.appendChild(elem2);
document.body.appendChild(fragment);
/**
* 3. Drag and Drop with Custom Images
* -----------------------------------
* You can customize the drag image by setting the `dragImage` property on the dataTransfer object.
*/
document.addEventListener("dragstart", (event) => {
const img = new Image();
img.src = "path/to/image.png";
event.dataTransfer.setDragImage(img, 10, 10);
});