아이프레임 크로스도메인 문제

https://junspapa-itdev.tistory.com/55

https://learn.microsoft.com/ko-kr/skype-sdk/ucwa/cross_domainiframe

부모

<iframe id="unseggun_ifr" src="https://www.unseggun.com?ref=herald" frameborder="0" style="width:100%;" scrolling ="no"></iframe>
<script>
    document.getElementById('unseggun_ifr').addEventListener('load', function(){
        document.getElementById('unseggun_ifr').contentWindow.postMessage({ data : 'iframe_height'}, 'https://www.unseggun.com');
        window.addEventListener('message', function(e) {
            if(e.origin == "https://www.unseggun.com"){
                if(e.data.data){
                    document.getElementById('unseggun_ifr').height = e.data.data;
                }
            }
        }, false);
    });
</script>

자식

<script>
window.addEventListener('message', function(e) { //요청온 메세지 확인.
    //허용된 도메인 리스트.
    var allow_domains = [
        'http://test.goodzz.site',
        'https://biz.heraldcorp.com'
    ];

    if(in_array(e.origin, allow_domains) == true){ //허용된 도메인 확인.
        //console.log(e.origin+" 허용된 도메인");
        if (e.data.data === 'iframe_height') { //요청온 데이터 값 확인.
            //요청한 곳으로 높이 값 전송.
            window.parent.postMessage({ data : document.body.scrollHeight }, e.origin);
            //console.log(e.origin+" 에게 값 전송 / "+document.body.scrollHeight);
        }
    }else{
        console.log(e.origin+" 허용되지 않은 도메인");
    }
});
</script>
guest
0 Comments
Inline Feedbacks
View all comments