javascript HTML 的“对象”标签是否有跨浏览器的标准加载事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7756353/
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
Is there a cross-browser standard on-load event for HTML's "object" tag?
提问by Hyangelo
I am aware that IE supports the onreadystatechange
attribute on the object
tag, but this doesn't seem to be a standard way thus all other browsers do not support it.
我知道 IE 支持标签onreadystatechange
上的属性object
,但这似乎不是标准方式,因此所有其他浏览器都不支持它。
Update: To clarify, I am not looking for the DOM Load event, I am looking for the load event of the object
tag itself(e.g. an object
tag embedding a PDF file into a page).
更新:澄清一下,我不是在寻找 DOM 加载事件,而是在寻找object
标签本身的加载事件(例如,object
将 PDF 文件嵌入到页面中的标签)。
In a way, I am looking for something similar to img
's onload
event /complete
attribute for the object
tag.
在某种程度上,我正在寻找类似于标签img
的onload
事件/complete
属性的东西object
。
回答by duri
The <object onload>
attribute works well in latest versions of Firefox, Chrome and Opera (I've just tested it).
该<object onload>
属性在最新版本的 Firefox、Chrome 和 Opera 中运行良好(我刚刚测试过)。
Also, the expression 'onload' in document.createElement('object')
evaluates to true
in Chrome and Opera; naturally, Firefox is an exception because such event support check doesn't work there generally. kangax describes cross-browser event support detection at http://perfectionkills.com/detecting-event-support-without-browser-sniffing/
此外,该表达式在 Chrome 和 Opera 中的'onload' in document.createElement('object')
计算结果为true
;自然,Firefox 是一个例外,因为此类事件支持检查通常在那里不起作用。kangax 在http://perfectkills.com/detecting-event-support-without-browser-sniffing/描述了跨浏览器事件支持检测
回答by RASG
So you want to trigger an event after the [PDF] object is loaded?
If so, this might help you:
那么你想在[PDF]对象加载后触发一个事件?
如果是这样,这可能会帮助您:
http://www.webdeveloper.com/forum/showthread.php?t=160792
http://www.webdeveloper.com/forum/showthread.php?t=160792
----- EDITED [19/10/2011] -----
----- 编辑 [19/10/2011] -----
If you load the PDF inside an iframe, it will work.
如果您在 iframe 中加载 PDF,它将起作用。
Check this: http://sykari.net/stuff/iframe
检查这个:http: //sykari.net/stuff/iframe
The script will alert only afterthe PDF is completely loaded.
(Tested with FF7 + Win7).
只有在PDF 完全加载后,脚本才会发出警报。
(使用 FF7 + Win7 测试)。
----- EDITED [20/10/2011] -----
----- 编辑 [20/10/2011] -----
Using JQuery: http://jsfiddle.net/fcABz
使用 JQuery:http: //jsfiddle.net/fcABz
To be sure it is working i have added a 36 pages PDF file to the iframe, so you can watch the progress bar (at least in FF7 there is one, not sure about Chrome) going up, and after it is fully loaded, you will see an alert.
为了确保它正常工作,我已将一个 36 页的 PDF 文件添加到 iframe,因此您可以观看进度条(至少在 FF7 中有一个,不确定 Chrome)正在上升,并且在它完全加载后,您将看到警报。
Why? Because the .loadmethod from JQuery will wait until all subelements inside the iframe are loaded.
为什么?因为JQuery的.load方法将等到 iframe 中的所有子元素都加载完毕。
The loadevent is sent to an element when it and all sub-elementshave been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.
所述负载事件被发送到一个元素时它和所有子元素已被完全加载。此事件可以发送到与 URL 关联的任何元素:图像、脚本、框架、iframe和 window 对象。
If you want to try more then once, clear the cache.
Or change the PDF to a bigger one, like this http://pubs.usgs.gov/tei/532/report.pdf(90 pages).
如果您想再试一次,请清除缓存。
或者将 PDF 更改为更大的 PDF,例如http://pubs.usgs.gov/tei/532/report.pdf(90页)。
回答by Jean-Charles
I'm not sure you can do this just using object attributes.
我不确定您是否可以仅使用对象属性来执行此操作。
Have a look at this :
看看这个:
You can try something using
onreadystatechange
When data is well loaded,
readyState==4
andxmlhttp.status==200
You can try to use jQuery load function:
Export your
<object/>
in a page calledobject.html
.In your main page include :
<div id="theObject"></div> <script> $("#theObject").load("object.html", function(response, status, xhr) { alert('Loaded : '+status);}); </script>
The message will be display when the request completes.
你可以尝试使用
onreadystatechange
当数据加载良好时,
readyState==4
并且xmlhttp.status==200
您可以尝试使用jQuery 加载功能:
将您的导出
<object/>
到名为object.html
.在您的主页中包括:
<div id="theObject"></div> <script> $("#theObject").load("object.html", function(response, status, xhr) { alert('Loaded : '+status);}); </script>
请求完成时将显示该消息。