问题
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