Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

blog

[iframe] 도메인이 다른 부모-자식 창 간의 통신 본문

Web/front-end

[iframe] 도메인이 다른 부모-자식 창 간의 통신

hjkongkong 2022. 3. 14. 19:47

Uncaught DOMException: Blocked a frame with origin "도메인" from accessing a cross-origin frame.

iframe에서 도메인이 다른 부모창 - 자식창 간의 통신(cross-origin error)

1
2
3
4
5
6
7
var data = "datatatatatata";
 
var iframe = document.getElementById("iframe_id");
 
var iframe_document = iframe.contentDocument || iframe.contentWindow;
 
iframe_document.postMessage(data,"http://localhost:56007자식주소");
cs

부모 javascript

 

1
2
3
4
5
6
7
8
       window.addEventListener("message", postMessageController,  true);
 
        function  postMessageController(e) {
            // 보안성으로 보낸 페이지 체크 한다.
            if (e.origin === 'http://localhost:부모주소') {
                alert(e.data); // 전송 받은 데이터
            }
        }
cs

자식 javascript

 

부모 창에서 alert 확인 가능