---
# System prepended metadata

title: iframe之間溝通
tags: [教學]

---

# iframe之間溝通

parent呼叫child網頁內的function
```
document.getElementById('iframe的ID').contentWindow.function名稱
```

反過來child呼叫parent網頁的function
```
parent.function名稱
```

PS. 前提是parent和child需要解決跨域的問題，失敗會看到如下:
:::danger
Uncaught DOMException: Blocked a frame with origin "https://xxx" from accessing a cross-origin frame
:::

參考: [iframe父頁子頁呼叫函式](https://blog.wu-boy.com/2010/05/%E7%AD%86%E8%A8%98-iframe-%E7%88%B6%E9%A0%81%E5%AD%90%E9%A0%81%E5%91%BC%E5%8F%AB%E5%87%BD%E5%BC%8F-parent-call-function/)

###### tags: `教學`