-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiframe.html
More file actions
35 lines (31 loc) · 916 Bytes
/
iframe.html
File metadata and controls
35 lines (31 loc) · 916 Bytes
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
<!DOCTYPE html>
<html>
<head>
<title>Two-way iframe</title>
</head>
<body>
<h1 id="title">Iframe ready</h1>
<button id="send">Send message to parent</button>
<script>
// 1️⃣ Receive messages from parent
window.addEventListener('message', (event) => {
console.log('Received from parent:', event.data);
if (event.data?.title) {
document.getElementById('title').innerText = event.data.title;
}
});
// 2️⃣ Send messages to parent
document.getElementById('send').addEventListener('click', () => {
window.parent.postMessage(
{ status: 'clicked', time: Date.now() },
window.location.origin
);
});
// 3️⃣ Notify parent that iframe is ready
window.parent.postMessage(
{ status: 'ready' },
window.location.origin
);
</script>
</body>
</html>