A data-* attribute on a <iframe> tag attaches additional data to the iframe element.
To create a custom attribute, replace * with a lowercase string, such as data-id
, data-status
, or data-location
.
A custom data-url
attribute on an <iframe>.
The attribute value is not visible, but it is readable by JavaScript.
<iframe src="https://en.wikipedia.org"
data-url="https://en.wikipedia.org"
width="99%">
</iframe>
The data-* attribute allows you to add custom attributes to a <iframe> element.
The * part is replaced with a lowercase string, such as data-id, data-cost, or data-location.
An <iframe> element can have any number of data-* attributes, each with their own name.
These attributes usually store additional data about the iframe (e.g. id, options, variations, etc.).
Using data-* attributes reduces the need for Ajax requests to the server.
Note: The data-* attribute does not change the appearance of the iframe tag in any way.
<iframe data-*="value">
Note: The * can be any string, such as data-id
, data-cost
, data-supplier
, etc.
Value | Description |
---|---|
value | A string value. Can be numeric, alphanumeric, JSON, etc. |
A custom data-link
attribute in an <iframe> element.
Clicking the button will display the link value.
<iframe id="myiframe" src="https://en.wikipedia.org"
data-link="Wikipedia English Edition"
style="height:350px;width:100%;">
</iframe>
<br/><br/>
<button onclick="show();">Show data</button>
<script>
let show = () => {
let element = document.getElementById("myiframe");
alert("Link = " + element.getAttribute('data-link'));
}
</script>
The <iframe> tag below contains a custom data-link
attribute.
The data-link
attribute specifies the description of the iframe URL.
Clicks are handled by the onclick
event.
Onclick invokes a JavaScript function that extracts and displays the iframe url description.
Note: Notice how the url will displays immediately without server call.
Here is when data-* support started for each browser:
Chrome
|
1.0 | Sep 2008 |
Firefox
|
1.0 | Sep 2002 |
IE/Edge
|
1.0 | Aug 1995 |
Opera
|
1.0 | Jan 2006 |
Safari
|
1.0 | Jan 2003 |
Back to <iframe>