Javascript取Flash对象
我本以为这个会很简单,不就是getElementById吗,不过自己试过才知道,在当今浏览器三分天下的情况下,加上一贯的Flash的奇怪的嵌入式语法(下面):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://..." WIDTH="150" HEIGHT="75" id="simplemovie" ALIGN=""> <PARAM NAME=movie VALUE="simplemovie.swf"> <PARAM NAME=quality VALUE=medium> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src="simplemovie.swf" quality=medium swliveconnect="true" bgcolor=#FFFFFF WIDTH="150" HEIGHT="75" name="simplemovie" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"> </EMBED> </OBJECT> |
这么简单的事情也变得不那么简单了,尤其是想做一个全兼容的方法,真的不简单。我用getElementById就死的很惨,因为在上面的这段代码中,getElementById一定会取到OBJECT对象,而Firefox是不认这个OBJECT的,它认的是下面的EMBED对象。
参阅了不少网页,最后我觉得这里的方法最为有理有据,值得参考。它用了6种方法,测试了4种浏览器,得到下面的结果:
IE6 Firefox 1 Opera 8 Netscape 7 window.document["simplemovie"]/window.document.simpleMovie Y Y Y Y document.simpleMovie /document["simpleMovie"] Y Y Y Y window["simplemovie"]/window.simplemovie Y N N N document.embeds.simplemovie/document.embeds["simplemovie"] N Y Y Y document.getElementById(”simplemovie”) Y N N N document.getElementsByName(”simplemovie”)[0] Y Y N Y
这里的Y不仅指能取到对象,而且要能操作到对象,显然前两种方法兼容性最好,而不是我以为的getElementById,我顺便测试了Firefox3和Chrome,Firefox3和Firefox1一样,而Chrome2则比Firefox多支持了getElementById,显然Chrome意识到getElementById如果取到上面的OBJECT对象,没有意义,修正了一下(因为Chrome没有历史版本兼容性问题,所以可以做到这点修正)。
最终的结果是,产生了这样的一个皆大欢喜的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function getFlashMovieObject(movieName) { if (window.document[movieName]) { return window.document[movieName]; } if (navigator.appName.indexOf("Microsoft Internet")==-1) { if (document.embeds && document.embeds[movieName]) return document.embeds[movieName]; } else { return document.getElementById(movieName); } } |
July 21st, 2009 in
code|编程
万分感谢!
已经花了四个小时的时间来发现和解决这个问题了,非常感谢!
Not exactly,if u design a website with asp.net, and the is included in the tag, we cannot get the flash object by ur method, so the resolution is document.forms[0].YOURFLASHOBJ_ID
And I think widnow.document.xxx is equal to document.xxx.
Thx.
I am sorry, I forget to tell that this problem is exist in IE only, Maybe we can treat this IE’s BUG.
thx.
@moon 是的,我给的函数是和你如何嵌入Flash对象相关的,在我给的那种方式下,确实是全兼容的。 你的Flash是放在Form下面的?所以要用你说的方法去修正,谢谢你的指出。
是的,window.document.xxx 是和 document.xxx 等价的。
@Y.S 看到你的感谢了,不用客气,我也是调试了很久才搞定,希望对有需要的人能帮上一点忙
[...] 这个我在前面的帖子里已经给了,对我来说比较常用,但可能不是每个人都觉得这个有用,取决于你用不用Flash对象。 [...]
[...] 文章来源:http://www.liuzhongshu.com/code/javascript-get-flash.html 你可能还会关心这些2010/03/11 — flash开发必备书籍 JS:textarea 开心个人心情输入框效果 jquery版 $("#loadpercent").animate({width:"67px"}) RSSMost Viewed [...]