References

Beginner-friendly references for web development, with live, editable examples.

The HTML <object> data attribute

Attribute All modern browsers
In short

Unlike the custom data-* attributes, the HTML data attribute on <object> holds the URL of a file to embed. With <object data="report.pdf">, the browser displays that file in place of the element, and shows the content between the tags when it can't.

Overview

The HTML data attribute gives the URL of the file that an <object> element embeds. That file can be a PDF, an image, an SVG drawing or another web page.

What appears depends on the file. Under the HTML standard, an image type such as PNG becomes an image, while HTML, SVG, PDF and other types load into a nested document, much like an <iframe>.

The content between <object> and </object> is fallback content. It shows only when the file can't be displayed, for example after a 404 response, and it can hold another object for the browser to try next.

The value is resolved like a link, so relative paths work, and the data property returns the full URL. That URL stays the same while the embedded page follows its own links, so it keeps naming the file the object loaded rather than the page now on view.

A page's Content Security Policy limits these URLs through its object-src directive. Under object-src 'none', the file is blocked, and the fallback content appears instead.

Syntax

<object data="annual-report.pdf" type="application/pdf"
  width="600" height="400">
  <a href="annual-report.pdf">Download the annual report (PDF)</a>
</object>

Values

Value
A URL pointing to the resource.

Example

Live example
<p>Press the button to change the first object's data. The second object's file is damaged.</p>
<object id="report" data="data:text/plain,May%20sales%20report" width="200" height="36">
  <p>The report could not be shown.</p>
</object>
<p><button type="button" id="june">Load the June report</button></p>
<object data="data:image/png;base64,AAAA" type="image/png" width="200" height="36">
  <p>Fallback text from the damaged image</p>
</object>
<p id="msg" role="status"></p>
<script>
const report = document.getElementById('report');
report.addEventListener('load', function () {
  const text = decodeURIComponent(report.data.slice(report.data.indexOf(',') + 1));
  document.getElementById('msg').textContent = 'The load event fired, and the object now shows "' + text + '".';
});
document.getElementById('june').addEventListener('click', function () {
  report.data = 'data:text/plain,June%20sales%20report';
});
</script>

Best practices

  • Put a download link inside an <object> whose data points to a PDF. Add a second link outside the element too, since a viewer can load and still fail to display the file.
  • An object that loads a page or a PDF into a nested document needs a width and height, or it's drawn at 300 by 150 pixels.
  • Serve the file from your own origin when a script needs to reach inside it. The object's contentDocument is null for a file from another origin.
  • Consider an <iframe> for embedding web pages. An iframe can cover almost all of object's use cases and offers more security features.

Frequently asked questions

What is the data attribute in an object tag?
The data attribute is the URL of the file an object element embeds, such as a PDF. The browser loads that URL and displays the file where the object sits.
Is the object data attribute the same as data-* attributes?
No, data on an object is a single attribute that holds a URL. Custom data-* attributes, such as data-id, store your own values on any element and are read through the dataset property.
Is the data attribute required on an object element?
Yes, the HTML standard requires a non-empty data URL, and without one its steps show the fallback. Firefox follows that rule, but an object with only a type shows no fallback in Chrome.
What events fire when the object data loads or fails?
An object fires load once its data file has loaded, or error when the request fails. For a nested page, the load event waits until that page has finished loading.
Does changing the data attribute reload the object?
Yes, giving data a new URL loads that file in place of the old one. Setting the same URL again reloads it too, because any assignment to data starts the loading steps over.
Can an object element load its data lazily?
No, object, unlike iframe, has no attributes that control when its data loads. The HTML standard says an object that isn't rendered doesn't load, a rule Chrome follows for display none and Firefox doesn't.