<embed> vs. <object>
Asked Answered
L

6

198

Which is the right/best tag to use in my HTML file when I want to display the Adobe PDF viewer?

Right now I'm using the code below, but there are weird side effects (e.g. it seems to steal the starting focus that I've set to another <input> text box; it doesn't seem to play real well with the jQueryUI Resizeable class; etc.)

<embed src="abc.pdf" type="application/pdf" />

Could I even do the same thing with the <object> tag? Are there advantages/disadvantages to using one tag vs. the other?

Lunge answered 7/8, 2009 at 13:47 Comment(2)
And now with <embed> in HTML5? What is the best way?Trigonometry
@Trigonometry <embed> is now officially a standard tag with HTML5, but you should anticipate at least some compatibility issues with older browser versions.Vocational
C
206

OBJECT vs. EMBED - why not always use embed?

Bottom line: OBJECT is Good, EMBED is Old. Beside's IE's PARAM tags, any content between OBJECT tags will get rendered if the browser doesn't support OBJECT's referred plugin, and apparently, the content gets http requested regardless if it gets rendered or not.

object is the current standard tag to embed something on a page. embed was included by Netscape (along img) before anything like object were on the w3c mind.

This is how you include a PDF with object:

<object data="data/test.pdf" type="application/pdf" width="300" height="200">
  alt : <a href="data/test.pdf">test.pdf</a>
</object>

If you really need the inline PDF to show in almost every browser, as older browsers understand embed but not object, you'll need to do this:

<object data="abc.pdf" type="application/pdf">
    <embed src="abc.pdf" type="application/pdf" />
</object>

This version does not validate.

Censor answered 7/8, 2009 at 14:0 Comment(11)
<embed> is actually part of HTML5 dev.w3.org/html5/spec/Overview.html#the-embed-elementEdea
Even though <embed> is part of HTML5 standard, it seems to me that <object> is the better choice because of compatibility with older browsers and the ability to display alternate content. Thoughts?Weismannism
@raphaelcm Allow me to play devil's advocate. If maintaining compatibility with outdated browsers was that important, HTML would never evolve. What's important here is market share, specifically regarding browser versions.Vocational
I landed here to see if I can find an alternative to iframe. Get the same issues that I get for iframe on a safari browser in a ios device: doesn't display first unless you force it to re-render and does not scroll.Polacre
HTML 5 should have wither object or embed tag. Both together don't have different semantics.Renata
Why is this accepted? I thought <embed> was the HTML5 standard tag.Israelite
I have tried above solution. It's working fine in firefox but not in chrome. In firefox only <object> element works but same in chrome shows alt msg in <object>alt: error </object>. And <embed> in chrome shows msg plugin not supported.Urumchi
I would think <embed> would be the better choice going forward. It's in the HTML5 standard while Object had many of it's features(attributes) deprecated to separate it's functionality from the embed tag. w3schools.com/tags/tag_object.asp It appears to me that the object tag is almost a 'Swiss army knife' tag while embed is purpose built for embedding content into a page.Gracious
One difference is that embed without a src yields an instance of the embedded feature. You can swap out the src attribute on an embed (with jquery for example), but you can't swap out the data attribute on an object.Nucleus
Does this work when I give the url of a pdf frile from internet ?Sardella
Trying to do this <object [data]="trustTwo" type="application/pdf"> <embed [src]="trustTwo" type="application/pdf" /> </object> but it says src(unknown) when I inspect it as nothing comes out its just blank. Any feedback, trying to grab the PDF from the local file its stored in 'trustTwo'.Achieve
E
15

Answer updated for 2020:

Both <object> and <embed> are included in the WHAT-WG HTML Living Standard (Sept 2020).


<object>

The object element can represent an external resource, which, depending on the type of the resource, will either be treated as an image, as a child browsing context, or as an external resource to be processed by a plugin.


<embed>

The embed element provides an integration point for an external (typically non-HTML) application or interactive content.


Are there advantages/disadvantages to using one tag vs. the other?

The opinion of Mozilla Developer Network (MDN) appears (albeit fairly subtly) to very marginally favour <object> over <embed> but, overwhelmingly, MDN wants to recommend that wherever you can, you avoid embedding external content entirely.

[...] you are unlikely to use these elements very much — Applets haven't been used for years, Flash is no longer very popular, due to a number of reasons (see The case against plugins, below), PDFs tend to be better linked to than embedded, and other content such as images and video have much better, easier elements to handle those. Plugins and these embedding methods are really a legacy technology, and we are mainly mentioning them in case you come across them in certain circumstances like intranets, or enterprise projects.

Once upon a time, plugins were indispensable on the Web. Remember the days when you had to install Adobe Flash Player just to watch a movie online? And then you constantly got annoying alerts about updating Flash Player and your Java Runtime Environment. Web technologies have since grown much more robust, and those days are over. For virtually all applications, it's time to stop delivering content that depends on plugins and start taking advantage of Web technologies instead.

Source: https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Other_embedding_technologies#The_%3Cembed%3E_and_%3Cobject%3E_elements

Eject answered 30/9, 2020 at 9:52 Comment(1)
There are other uses for both than plugins. SVG (which can also use <svg> or <img> with subtle differences...) and PDF are two common ones.Myself
P
6

Some other options:

<object type="application/pdf" data="filename.pdf" width="100%" height="100%">
</object>

<object type="application/pdf" data="#request.localhost#_includes/filename.pdf" 
        width="100%" height="100%">
  <param name="src" value="#request.localhost#_includes/filename.pdf">
</object>
Poachy answered 4/2, 2010 at 1:28 Comment(0)
T
4

You could also use the iframe method, although this is not cross browser compatible (eg. not working in chromium or android and probably others -> instead prompts to download). It works with dataURL's and normal URLS, not sure if the other examples work with dataURLS (please let me know if the other examples work with dataURLS?)

 <iframe class="page-icon preview-pane" frameborder="0" height="352" width="396" src="data:application/pdf;base64, ..DATAURLHERE!... "></iframe>
Tirewoman answered 24/7, 2014 at 17:18 Comment(0)
T
4

Probably the best cross browser solution for pdf display on web pages is to use the Mozilla PDF.js project code, it can be run as a node.js service and used as follows

<iframe style="width:100%;height:500px" src="http://www.mysite.co.uk/libs/pdfjs/web/viewer.html?file="http://www.mysite.co.uk/mypdf.pdf"></iframe>

A tutorial on how to use pdf.js can be found at this ejectamenta blog article

Tirewoman answered 18/12, 2015 at 10:45 Comment(0)
S
2

Embed is not a standard tag, though object is. Here's an article that looks like it will help you, since it seems the situation is not so simple. An example for PDF is included.

Scrofulous answered 7/8, 2009 at 14:1 Comment(2)
Embed seems to be quite standard to me - at least in HTML5.Crave
@bažmegakapa It is certainly standard now with HTML5, but that article he is referring to was written back in 2008 and his answer is from 2009, which predates HTML5.Vocational

© 2022 - 2024 — McMap. All rights reserved.