Scripte Iframe



To change the iframe src attribute, we will be using a select drop down menu, and a button element to activate the function that will change the src attribute. Note that this is only for example purposes, instead of using onClick you may want to consider using event handlers in an external script. The below is a fully working example. An iFrame is a way to embed a web page into the content of another page. This is done using HTML tags, the external page’s URL, and specifications regarding how the window should appear on your site. There are two situations in which iFrames are generally useful. The first is when you wish to share content that doesn’t belong to you.

The iframe is hidden off-screen, so the browser user won’t have any idea that they just “visited” the example.com page. However, this attack is effectively the same as a conventional XSS attack, since the attacker could have simply redirected the user directly to the example.com page, using a variety of methods, including a meta element like this (again, the meta element’s URL is.

In this tutorial you will learn how to use an iframe to display a web page within another web page.

What is iframe

An iframe or inline frame is used to display external objects including other web pages within a web page. An iframe pretty much acts like a mini web browser within a web browser. Also, the content inside an iframe exists entirely independent from the surrounding elements.

The basic syntax for adding an iframe to a web page can be given with:

The URL specified in the src attribute points to the location of an external object or a web page.

The following example display 'hello.html' file inside an iframe in current document.

Script iframe php

Setting Width and Height of an iFrame

Best folder color software for mac. The height and width attributes are used to specify the height and width of the iframe.

Javascript iframe

You can also use CSS to set the width and height of an iframe, as shown here:

Please see the tutorial on HTML styles to learn the methods of applying CSS to HTML elements.

Script iframe html

Note: The width and height attribute values are specified in pixels by default, but you can also set these values in percentage, such as 50%, 100% and so on. The default width of an iframe is 300 pixels, whereas the default height is 150 pixels.

Removing Default Frameborder

The iframe has a border around it by default. However, if you want to modify or remove the iframe borders, the best way is to use the CSS border property.

The following example will simply render the iframe without any borders.

Script Iframe Src

Similarly, you can use the border property to add the borders of your choice to an iframe. The following example will render the iframe with 2 pixels blue border.

Using an iFrame as Link Target

An iframe can also be used as a target for the hyperlinks.

An iframe can be named using the name attribute. This implies that when a link with a target attribute with that name as value is clicked, the linked resource will open in that iframe.

Let's try out an example to understand how it basically works:

Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more
Attribute of
Using The HTML Tag To Create Inline Frames: Here's How
What does <iframe sandbox='> do?
Places a set of security and usability restrictions on the iframe.

Contents

Code Example

In the source document, the video starts playback automatically. But in sandbox mode, the video does not autoplay.

Restricting and re-enabling

Javascript Iframe

Adding the sandbox attribute to an <iframe> element places the element into sandbox mode, which adds the following restrictions to how the browser treats the document inside the iframe:

  • iframe content is treated as being from a different origin than the primary document
  • blocks form submission
  • block JavaScript execution
  • disable Pointer Lock API (mouse movement capture)
  • prevents anchor links from targeting other browsing contexts (opening in a new tab or window)
  • prevents use of plugins (<embed>, <object>, <applet>, or other)
  • prevent the content from navigating its top-level browsing context
  • blocks auto playback of media

These sandbox precautions can help maintain browser security, which is especially useful if the <iframe> is embedding a third-party document over which you have no control.

Each of these restrictions can be lifted individually using attribute values.

Content Script Iframe

Values of the sandbox Attribute

Value NameNotes
allow-top-navigationAllows a sandboxed iframe to navigate its top-level browsing context
allow-scriptsRe-enables JavaScript in a sandboxed iframe.
allow-same-originAllows the content of a sandboxed iframe to be treated as having the same origin as the primary document.
allow-popupsRe-enables popups in a sandboxed iframe.
allow-pointer-lockRe-enables the Pointer Lock API (mouse movement capture) in sandboxed a iframe.
allow-formsRe-enables form submission in a sandboxed iframe.

Script Iframe Html

All Attributes of iframe Element

Attribute nameValuesNotes
sandboxallow-top-navigation
allow-scripts
allow-same-origin
allow-popups
allow-pointer-lock
allow-forms
Places a set of security and usability restrictions on the iframe.
scrollingWas used to toggle scrolling on iframes. Deprecated in HTML5. Use CSS instead.
nameSpecifies the name of an iframe.
alignWas used to set the alignment of an inline frame relative to surrounding elements. Deprecated. Use CSS instead.
frameborderWas used to toggle the display of a border around an iframe. Deprecated in HTML5. Use CSS instead.
longdescWas used to specify URL containing a long description of an iframe. Deprecated in HTML5. Use CSS instead.
marginwidthWas used to control the width of margins around an iframe. Deprecated in HTML5. Use CSS instead.
srcSpecifies the URL of a document to display in an iframe.
vspaceWas used to control the vertical spacing around an iframe. Deprecated in HTML5. Use CSS instead.
widthSpecifies the width of an iframe.

Script Iframe Redirect

Browser Support for sandbox