-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
66 lines (59 loc) · 1.84 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#switch {
height: 4em;
width: 10em;
background-color: coral;
}
#switch:hover {
background-color: pink;
}
</style>
</head>
<body>
<input type="button" id="switch" value="Light ON">
<div id="feedback">on or off</div>
</body>
<script>
function sendRequest(target, action, value="") {
console.log("making request")
let xR = new XMLHttpRequest();
xR.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("Server Response:", this.responseText);
data = JSON.parse(this.responseText);
feedback.innerText = data['status'];
d.getElementById("switch").value = `Light ${data['status']}`;
}
}
let data = {};
data["action"] = action;
data["value"] = value;
xR.open("POST", target, true);
xR.send(JSON.stringify(data));
}
d=document;
switchstatus=true;
onbutton=d.getElementById("switch");
onbutton.addEventListener("click",toggle);
function toggle(){
feedback=d.getElementById("feedback");
if (switchstatus){
switchstatus=false;
feedback.innerHTML="off"
console.log("turning it off");
sendRequest("led",action="OFF");
} else {
switchstatus=true;
feedback.innerHTML="on"
console.log("turning it on");
sendRequest("led",action="ON");
}
}
</script>
</html>