In HTML, the data attribute is used with the <object> element to specify the URL of an external resource to embed — such as a PDF, image, or other file. It tells the browser where to fetch the content displayed inside the object.
What You’ll Learn
01
Resource URL
Points to file.
02
object Tag
Primary element.
03
type MIME
Pair with data.
04
Fallback
Inner content.
05
object.data
JS property.
06
vs data-*
Not the same.
Fundamentals
Purpose of data
The main purpose of the data attribute is to specify the address of the resource used by the <object> element. That resource can be a PDF document, SVG file, image, or other embeddable content. The attribute lets you integrate external files into your page through a single tag.
💡
Not for custom element metadata
Attributes like data-id or data-price on a <div> are data-* custom attributes — a different feature covered in the next tutorial. The standalone data attribute is only for <object> resource URLs.
In this example, data specifies the URL of the PDF file that the <object> element embeds. The type attribute tells the browser what kind of file to expect.
Example with Fallback
Let’s explore a more comprehensive example using an image with fallback content:
data-image.html
<objectdata="photo.png"type="image/png"width="300"height="200"><!-- Fallback if object cannot render the data URL --><imgsrc="fallback-image.jpg"alt="Fallback Image"></object>
The data attribute points to the primary image. Fallback content inside <object> ensures something visible appears when the browser cannot render the embedded resource. Match type to the actual file format (PNG, not JPEG, for .png files).
Dynamic Values with JavaScript
You can dynamically set the data attribute using JavaScript to change embedded content based on user interactions:
dynamic-data.html
<objectid="dynamicObject"data="chart-a.svg"type="image/svg+xml"width="200"height="150"></object><script>
var objectElement = document.getElementById("dynamicObject");
objectElement.data = "chart-b.svg";
</script>
In this script, the data property of an object element with id dynamicObject is updated to point to a new resource URL. This dynamic flexibility helps build interactive pages that swap embedded files without reloading.
A11y
♿ Accessibility
Meaningful fallback — Provide descriptive text or download links inside <object>, not just “unsupported.”
Alt on fallback images — If fallback uses <img>, include proper alt text.
Prefer native elements — For images, video, and audio, native tags offer better accessibility than object embeds.
Title attribute — Consider title on object for a brief description of embedded content.
Keyboard access — Ensure fallback links and controls are keyboard reachable.
🧠 How data Works
1
Author sets data URL
object data points to PDF, image, or other file.
Markup
2
Browser fetches resource
type helps select the correct plugin or handler.
Load
3
Content renders or falls back
Inner HTML displays if embed is unsupported.
Fallback
=
📄
External resource embedded
One tag loads files beyond basic HTML markup.
Compatibility
Browser Support
The data attribute on <object> is supported in all modern browsers. What actually renders depends on the file type and installed plugins (PDF viewers vary by browser).
✓ HTML · Fully supported
Universal object data attribute
All major browsers parse data on object elements.
99%Attribute support
Google ChromeFully supported
Full support
Mozilla FirefoxFully supported
Full support
Apple SafariFully supported
Full support
Microsoft EdgeFully supported
Full support
data attribute on object99% supported
Bottom line: The attribute works everywhere; test PDF and plugin rendering per browser. Prefer native HTML5 tags when possible.
Pro Tips
💡 Best Practices
✅ Do
Use data on object for PDFs and specialized embeds
Set type to the correct MIME type
Include fallback HTML inside object
Prefer img, video, and audio for simple media
Test embedded content across browsers
❌ Don’t
Confuse data with data-* custom attributes
Use object for Flash or obsolete plugins
Mismatch type and file extension
Leave object empty with no fallback
Use data on div or span for JS metadata
Ensure the resource in data is compatible with the <object> element and the user’s browser.
Provide fallback content within <object> to maintain visibility when embed fails.
Regularly test embedded content across different browsers for a consistent experience.
Wrap Up
Conclusion
The data attribute is a valuable tool when working with the <object> tag in HTML. It lets you point at external resources and create pages that embed PDFs, images, and other files beyond what basic tags alone provide.
Remember that this attribute is specifically for object resource URLs. To store custom values on any element for JavaScript, use data-* attributes covered in the next tutorial.