Are there any key value system in post message in iframe communication

◇◆丶佛笑我妖孽 提交于 2021-02-08 11:46:15

问题


My iframe Code Link is http://example.org?pd=123

            var currentHeight = $('.comments_holder').outerHeight();
            var parent = window.parent;
            var targetOrigin = "my parent site link";
            if (parent && parent.postMessage) {
                parent.postMessage(currentHeight, targetOrigin);
            }

From this i need to pass the height of the div to the main site

And now i also want to pass another parameter to main site

                var count = 123;
                var parent = window.parent;
                var targetOrigin = "my parent site link";
                if (parent && parent.postMessage) {
                    parent.postMessage(count , targetOrigin);
                }

And my code in main site i.e. single.php of wordpress site

<script>
jQuery(document).ready(function($){
window.addEventListener('message', receiveMessage, false);

function receiveMessage(event)
{

what will if else condition here to get both the values 

} 

});
</script>

NOTE i cant check based on event.origin as the link are containing parameters for eg example.com?pd=123

And this link could be changing


回答1:


You'd pass the set of items through as an object:

parent.postMessage({'height': currentHeight, 'stuff': 'foo'}, targetOrigin);

In your example "event.data" would contain the passed object. For example:

function receiveMessage(event) {
   console.log(event.data.height);
}

See https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage#The_dispatched_event for more detail.



来源:https://stackoverflow.com/questions/25215941/are-there-any-key-value-system-in-post-message-in-iframe-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!