iFrame: Using the Window.postMessage() Framework
  • 1 Minute to read
  • Dark
    Light

iFrame: Using the Window.postMessage() Framework

  • Dark
    Light

Article summary

Window.postMessage() Framework:

When embedding content in a Syndigo inline-frame widget, there are 2 messages you can send to the parent frame:

  • Adjusting Content Height
  • Visiting a URL in Parent Frame

Adjusting Content Height

A quick example showing how you might send content height values to the parent frame whenever the content size changes:

const resizeObserver = new ResizeObserver(entries => {
    const body   = document.body;
    const html   = document.documentElement;
        
    const height = Math.max(body.scrollHeight, body.offsetHeight,
                            html.clientHeight, html.scrollHeight, html.offsetHeight);
    window.parent.postMessage(JSON.stringify({
        messageType: 'heightUpdateRequest',
        height
    }), '*');
});

const mainElm = document.querySelector('main');
resizeObserver.observe(mainElm);

assuming your content leverages a standard body HTML element where the content of your page is present, you can copy / paste the above code into the head or top of the body and it will function as expected. If your content does not leverage a standard body HTML element where content is present then you will need to alter the provided example code to support your page.

Visiting a URL in Parent Frame

These only work if your links point at pages on the same site. (can't navigate to a different site)

window.parent.postMessage(JSON.stringify({
    messageType: 'visitLink',
    href: 'CONTENT_URL_HERE'
}), '*');

Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.
ESC

Eddy AI, facilitating knowledge discovery through conversational intelligence