IFrames
suggest changeParameters
Attribute | Details |
——— | —–– |
name | Sets the element’s name, to be used with an a tag to change the iframe’s src. |width | Sets the element’s width in pixels. |height | Sets the element’s height in pixels. |src | Specifies the page that will be displayed in the frame. |srcdoc | Specifies the content that will be displayed in the frame, assuming the browser supports it. The content must be valid HTML. |sandbox | When set, the contents of the iframe are treated as being from a unique origin and features including scripts, plugins, forms and popups will be disabled. Restrictions can be selectively relaxed by adding a space-separated list of values. See the table in Remarks for possible values. |allowfullscreen | Whether to allow the iframe’s contents to use requestFullscreen() |
Remarks
An iframe is used to embed another document in the current HTML document.
You CAN use iframes for displaying:
- other HTML pages on the same domain;
- other HTML pages on another domain (see below - Same-origin policy);
- PDF documents (though IE might have some problems, This SO question might help);
You SHOULD use an iframe as a last resort, as it has problems with bookmarking and navigation, and there are always better options other than an iframe. This SO question should help you understand more about the ups and downs of iframes.
Same-origin policy
Some sites cannot be displayed in an iframe because they send X-Frame-Options or a Content-Security-Policy frame-ancestors directive. That is not the same-origin policy. The same-origin policy does allow a cross-origin page to be shown in an iframe; it prevents the parent page from reading or changing that iframe’s document.
If the iframe loads a different origin, scripts in the parent cannot access or manipulate the content inside the iframe.
The iframe element on W3C
sandbox attribute
The sandbox attribute, when set, adds extra restrictions to the iframe. A space separated list of tokens can be used to relax these restrictions.
Value | Details |
—– | —–– |allow-forms | Allows forms to be submitted. |allow-pointer-lock | Enables the JavaScript pointer API. |allow-popups | Popups can be created using window.open or <a target="_blank" |allow-same-origin | The iframe document uses its real origin instead of being given a unique one. If used with allow-scripts the iframe document can remove all sandboxing if it’s from the same origin as the parent document. |allow-scripts | Enables scripts. The iframe document and parent document may be able to communicate with each other using the postMessage() API. If used with allow-same-origin the iframe document can remove all sandboxing if it’s from the same origin as the parent document. |allow-top-navigation | Allows the iframe’s content to change the location of the top level document. |