iframe通信中的帖子中是否有任何键值系统 [英] Are there any key value system in post message in iframe communication
问题描述
我的iframe代码链接为 http://example.org?pd=123
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);
}
因此,我需要将div的高度传递到主站点
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);
}
还有我在主要网站中的代码,即wordpress网站的 single.php
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>
请注意,由于链接包含参数,我无法基于event.origin进行检查例如 example.com?pd=123
NOTE i cant check based on event.origin as the link are containing parameters
for eg example.com?pd=123
此链接可能正在更改
推荐答案
您需要将一组项目作为对象传递通过:
You'd pass the set of items through as an object:
parent.postMessage({'height': currentHeight, 'stuff': 'foo'}, targetOrigin);
在您的示例中,"event.data"将包含传递的对象.例如:
In your example "event.data" would contain the passed object. For example:
function receiveMessage(event) {
console.log(event.data.height);
}
请参见 https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage#The_dispatched_event 了解详情.
这篇关于iframe通信中的帖子中是否有任何键值系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!