cross site iframe postMessage from child to parent

后端 未结 2 1027
北荒
北荒 2020-12-16 03:00

I found this samplefrom SO while browsing for my problem, but I want to know exactly how to use it in my scenario. I have an iframe which is from another domain, and I want

2条回答
  •  猫巷女王i
    2020-12-16 03:19

    You need to set targetOrigin when using postMessage.

    
    

    Then on the host page.

    function addAnEventListener(obj,evt,func){
        if ('addEventListener' in obj){
            obj.addEventListener(evt,func, false);
        } else if ('attachEvent' in obj){//IE
            obj.attachEvent('on'+evt,func);
        }
    }
    
    function iFrameListener(event){
         secondPageValue = event.data;
    }
    
    var secondPageValue='';
    
    addAnEventListener(window,'message',iFrameListener);
    

提交回复
热议问题