Explain Codes LogoExplain Codes Logo

Difference between iframe, embed and object elements

html
responsive-design
postmessage
mixed-content
Nikita BarsukovbyNikita Barsukov·Feb 4, 2025
TLDR

<iframe> - This HTML element is a framework allowing you to view an entire webpage within your page. Perfect for embedding maps or videos directly.

<iframe src="https://example.com" title="Example"></iframe>

<embed> - Specifically designed for incorporating plugin content such as PDF readers or Flash animations. It's a single tag, with no closing.

<embed src="media.mp4" type="video/mp4" />

<object> - A jack-of-all-trades, an <object> can display a wide variety of media types, even applets. Custom parameters can be added via <param>.

<object data="animation.swf" width="400" height="300"> <param name="movie" value="animation.swf" /> </object>

The choice between these three relates to your specific requirements for compatibility, and the behavior you want from the embedded content.

Understanding each tag's attributes

Choosing <iframe> for live interaction

An <iframe> is excellent for interactive content, such as forms or applications. The parent document and the child <iframe> can have a two-way communication via scripts or postMessage API. This opens up possibilities for dynamic and real-time interactions with users. When security is a concern, you can restrict actions by sandboxing:

<iframe src="https://example.com" sandbox="allow-forms allow-scripts"></iframe>

But remember, power comes with risks. Always double-check the sources you are integrating into your project, and employ security measures to prevent dangerous activities such as clickjacking.

How <object> accommodates diverse media

The <object> tag is your one-stop solution for varied media types - images, flash content, even overlays of HTML documents. It provides a powerful fallback mechanism if the main resource fails:

<object data="image.svg" type="image/svg+xml"> <img src="image.png" alt="Fallback Image" /> </object>

When it comes to responsive design, <object> has got your back!

Using <embed> for plugin content

Although the use of <embed> has dwindled with the fading of many browser plugins, this rootin' tootin' old-timer can still pack a punch when it comes to SVG and specific HTML content:

<embed src="document.svg" type="image/svg+xml" />

This less frequently used tag enables communication between documents, albeit not as robustly as its cousin <iframe>.

The Do's and Don'ts of cross-document communication

Asynchronous communication with postMessage

When it's time for your parent and child documents to have "the talk", postMessage API enables secure asynchronous messaging across the same and cross-origin documents:

// Parent window: talking to the child iframe var iframe = document.getElementById('chloetheiframe'); // Who names their iframe Chloe? iframe.contentWindow.postMessage('Hey Chloe, bring more coffee!', 'https://example.com'); // Chloe is always out of coffee // Embedded iframe: listening to parent messages window.addEventListener('message', function(event) { if (event.origin === 'https://parentdomain.com') { // Always verify the sender for security reasons! console.log('Heck, we are out of coffee again :(', event.data); } }, false);

This method works for both <iframe> and <object>, offering a data exchange mechanism without exposing users to potential risks.

Preventing mixed content warning

To avoid mixed content warnings (aka the bane of web developers' lives), both <iframe> and <object> should include only HTTPS sources:

<iframe src="https://goodoldexample.com"></iframe> <object data="https://betterexample.com"></object>

In an era of increasing scrutiny on web security, SSL/TLS form a critical feature for serving web content.

Taming older content and plugins

For legacy media types or supporting browsers of yesteryears, <object> can prove suitable:

<object data="antique-content.swf"> <!-- Your fallback content --> </object>

While <object> can handle such cases, remember, older technologies not only lack support but often pose security risks. Where possible, consider transitioning to modern alternatives.