ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

HTML5 Tutorial• 6 min read

HTML iFrames, Sandbox & Web APIs

Embedding third-party content securely and native browser APIs

The <iframe> tag embeds an isolated nested browsing context inside your document. Modern HTML mandates sandbox security attributes to protect against malicious embeds.

Securing iFrames with the Sandbox Attribute

<iframe src="...">: Renders an external webpage inside a rectangular window on your page.

The sandbox attribute restricts the embedded page: it disables script execution, form submissions, popups, and cookie access unless explicitly allowed.

Permissive tokens: sandbox="allow-scripts allow-same-origin" grant fine-grained permissions.

loading="lazy": Defers loading off-screen iframes until the user scrolls near them, dramatically saving network bandwidth.

Secure Sandboxed YouTube / Map Embed
<iframe 
  width="560" 
  height="315" 
  src="https://www.youtube.com/embed/dQw4w9WgXcQ" 
  title="Product Overview Video" 
  sandbox="allow-scripts allow-same-origin allow-presentation" 
  loading="lazy" 
  referrerpolicy="strict-origin-when-cross-origin" 
  allowfullscreen>
</iframe>
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

What is the purpose of the `sandbox` attribute on an HTML `<iframe>` element?