I have this code inside a iframe:
window.addEventListener('message', function(e){
if(e.data == 'test')
console.log(e);
}, false);
and this inside the parent document:
$('#the_iframe').get(0).contentWindow.postMessage('test', 'http://localhost/');
So the parent document sends a “test” message to the iframe and it works.
But how can I define a function in the parent document, and somehow send this function through postMessage to the iframe, which will execute the function locally?
The function does some changes to the document like this:
var func = function(){
$("#some_div").addClass('sss');
}
(#some_div exists in the iframe, not the parent document)
There’s nothing that would prevent you from passing a stringified function as postmessage event data. Implementation is trivial, for any function declaration like
You could
encodeURIits string interpretation:It can then be executed as part of a closure – something not overly imaginative like
There’s a fiddle’d proof of concept without the cross-frame architecture – I’ll see if I can put together a postMessage version, but it would be non-trivial to host w/jsfiddle
Update
As promised, a full mockup that works (links below). With correct
event.originchecks this would be sufficiently inpenetrable, but I know for the fact that our security team would never letevalinto production like this 🙂Given the option I’d suggest the functionality be normalized across the two pages so that only a parametric message would need to be passed (i.e. pass arguments not functions); however there are definitely a few scenarios where this is a preferred approach.
Parent code:
Iframe code:
Prototype mockup: parent code and iframe code.