Access parent iframe with postMessage

less than 1 minute read

Rarely someone would need to use postMessage across nested iframes in cross domain scenario, but I came across this the other day:

<iframe src="//" name="www">
    <!-- nested body -->
    <iframe src="//" name="foo">
        <!-- another nested body -->
        <iframe src="//" name="nested">
                // I want to postMessage to

You can access the parent iframe just as if it wasn’t on a different subdomain. But the caveat is that the parent’s parent frame cannot be accessed.

// within iframe[name=nested]
parent.postMessage("some message", "");

If access to iframe[name=www] is necessary, and is the topmost frame, messages can be sent with:

// within iframe[name=nested]
top.postMessage("some message", "");

Leave a Comment