-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
165 lines (147 loc) · 5.15 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Window postMessage Example</title>
<style>
* {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
code {
font-family: monospace;
background-color: rgb(87, 87, 87);
color: white;
padding: 5px;
border-radius: 10px;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 0;
padding: 0 0 20px 0;
background-color: rgb(240, 240, 240);
}
#parentToChild {
display: flex;
justify-content: space-between;
align-items: center;
border: 1px solid rgb(54, 54, 54);
border-radius: 5px;
width: 95%;
padding: 10px 20px;
}
#childToParent {
display: flex;
justify-content: space-between;
align-items: center;
border: 1px solid rgb(54, 54, 54);
border-radius: 5px;
width: 95%;
padding: 10px 20px;
}
#messageToBeSent {
padding: 10px;
border: 1px solid rgb(54, 54, 54);
border-radius: 5px;
width: 95%;
}
button {
display: block;
margin-top: 10px;
padding: 10px;
background-color: rgb(0, 110, 255);
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color: rgb(0, 90, 255);
}
button:active {
background-color: rgb(0, 70, 255);
}
iframe {
margin-top: 10px;
border: 1px solid rgb(54, 54, 54);
border-radius: 5px;
width: 100%;
height: 100%;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#parentReceivedMessage {
margin-top: 10px;
padding: 10px;
border: 1px solid rgb(54, 54, 54);
border-radius: 5px;
background-color: white;
}
</style>
</head>
<body>
<h1><i>From Parent to Child (iframe)</i></h1>
<div id="parentToChild">
<div style="width: 40%;">
<h1>Parent HTML</h1>
<h2>This is the parent HTML that encloses the <code>child.html</code> iframe</h2>
<input id="messageToBeSent" type="text" placeholder="Enter your message to child iframe...">
<button id="sendButton">Send Message to Child</button>
</div>
<div style="width: 10%;">
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="lucide lucide-chevrons-right">
<path d="m6 17 5-5-5-5" />
<path d="m13 17 5-5-5-5" />
</svg>
</div>
<div style="width: 40%;">
<iframe src="child.html" frameborder="1" style="height: 300px;"></iframe>
</div>
</div>
<h1><i>From Child (iframe) to Parent</i></h1>
<div id="childToParent">
<div style="width: 40%;">
<h1>Parent HTML</h1>
<h2>This is the parent HTML that encloses the <code>child.html</code> iframe</h2>
<div id="parentReceivedMessage"> </div>
</div>
<div style="width: 10%;">
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="lucide lucide-chevrons-left">
<path d="m11 17-5-5 5-5" />
<path d="m18 17-5-5 5-5" />
</svg>
</div>
<div style="width: 40%;">
<iframe src="child2.html" frameborder="0" style="height: 350px;"></iframe>
</div>
</div>
<script>
const iframe = document.querySelector('iframe');
const messageToBeSent = document.getElementById('messageToBeSent');
const sendButton = document.getElementById('sendButton');
const sendMessageToChild = () => {
const message = {
message: messageToBeSent.value
}
iframe.contentWindow.postMessage(message, "*");
}
sendButton.addEventListener("click", sendMessageToChild);
messageToBeSent.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
sendMessageToChild();
}
});
window.addEventListener("message", (event) => {
const messageDiv = document.getElementById("parentReceivedMessage");
messageDiv.textContent = event.data.message;
})
</script>
</body>
</html>