sandbox

The sandbox attribute puts extra limits on the page inside an iframe. An empty sandbox blocks scripts, forms, popups, and top-level navigation, and gives the page a unique origin. Each token you add gives one power back.

HTML attribute

Updated

On this page

How it works

Add sandbox with no value to get the strictest limits. The framed page can still show HTML and CSS. It cannot run scripts, submit forms, open popups, or change the URL of the top page. Its origin is unique, so it cannot read its own cookies or localStorage.

Each allow-* token removes one limit. The sandbox only removes powers. It cannot give a frame a power that a header or the browser already blocks.

Tokens

TokenWhat it gives back
allow-scriptsRun JavaScript.
allow-same-originKeep the real origin, with its cookies and storage.
allow-formsSubmit forms.
allow-popupsOpen new windows and tabs.
allow-popups-to-escape-sandboxOpen popups without the sandbox limits.
allow-modalsUse alert(), confirm(), prompt(), and print().
allow-downloadsStart file downloads.
allow-top-navigation-by-user-activationChange the top page URL after a click or tap.
allow-top-navigationChange the top page URL at any time.

Other tokens are allow-pointer-lock, allow-orientation-lock, allow-presentation, allow-top-navigation-to-custom-protocols, and allow-storage-access-by-user-activation.

Example

HTML
<!-- All limits: no scripts, no forms, no popups -->
<iframe src="https://widget.example/ad" sandbox title="Ad"></iframe>

<!-- Scripts and forms only -->
<iframe src="https://widget.example/form" sandbox="allow-scripts allow-forms" title="Form"></iframe>

Tokens are separated by spaces. Each token removes one limit.

How to set it

Start with an empty sandbox. Then add only the tokens the framed page needs.

<iframe
  src="https://widget.example/form"
  sandbox="allow-scripts allow-forms allow-popups"
  title="Partner form"
></iframe>

Do not use allow-scripts with allow-same-origin when the frame has the same origin as your page. The frame can then remove its own sandbox.

Learn more

Sources

Know before you embed.

Free, no sign-up, nothing stored. Paste a URL and get a verdict, a live preview, and the fix.