Html <embed> 与 <object>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1244788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<embed> vs. <object>
提问by JayhawksFan93
Which is the right/best tag to use in my HTML file when I want to display the Adobe PDF viewer?
当我想显示 Adobe PDF 查看器时,哪个是在我的 HTML 文件中使用的正确/最佳标签?
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.)
现在我正在使用下面的代码,但是有一些奇怪的副作用(例如,它似乎窃取了我设置到另一个<input>
文本框的起始焦点;它似乎与 jQueryUI Resizeable 类并不能很好地配合;等等。)
<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?
我什至可以对<object>
标签做同样的事情吗?使用一种标签与另一种标签有什么优点/缺点吗?
回答by Esteban Küber
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 是好的,EMBED 是旧的。除了 IE 的 PARAM 标签之外,如果浏览器不支持 OBJECT 的引用插件,则 OBJECT 标签之间的任何内容都将被呈现,显然,无论是否呈现,内容都会被 http 请求。
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 w3cmind.
object
是当前在页面上嵌入某些内容的标准标签。在w3c想到任何类似的东西之前,它就embed
被 Netscape(连同img
)包括在内。object
This is how you include a PDF with object
:
这就是您包含 PDF 的方式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 needthe inline PDF to show in almost every browser, as older browsers understand embed
but not object
, you'll need to do this:
如果您真的需要在几乎所有浏览器中显示内联 PDF,正如旧浏览器所理解embed
但不是object
,您需要执行以下操作:
<object data="abc.pdf" type="application/pdf">
<embed src="abc.pdf" type="application/pdf" />
</object>
This version does not validate.
此版本不验证。
回答by aguz
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>
回答by ejectamenta
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 方法,尽管这不是跨浏览器兼容的(例如,不适用于 Chrome 或 android 以及其他可能 -> 而是提示下载)。它适用于 dataURL 和普通 URLS,不确定其他示例是否适用于 dataURLS(请告诉我其他示例是否适用于 dataURLS?)
<iframe class="page-icon preview-pane" frameborder="0" height="352" width="396" src="data:application/pdf;base64, ..DATAURLHERE!... "></iframe>
回答by ejectamenta
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
在网页上显示 pdf 的最佳跨浏览器解决方案可能是使用 Mozilla PDF.js 项目代码,它可以作为 node.js 服务运行并使用如下
<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
在此ejectamenta 博客文章中可以找到有关如何使用 pdf.js 的教程
回答by aehlke
Embed is not a standard tag, though object is. Here's an articlethat looks like it will help you, since it seems the situation is not so simple. An example for PDF is included.
Embed 不是标准标签,但 object 是。这是一篇看起来对您有所帮助的文章,因为情况似乎并不那么简单。包含一个 PDF 示例。