【IE9】iframe中嵌入flash,会有内存泄漏问题

引:http://stackoverflow.com/a/18453540

[quote]
This appears to be an IE9-only bug.

It occurs when a Flash object interacts with a HTML document using JavaScript (ExternalInterface on the Flash/ActionScript-side) and rears it's ugly head when an IFRAME containing the HTML document w/ the Flash Object comes into play.

Seeing how you specify you're using the YouTube API, there is sadly nothing you can do to make sure the Flash unregisters itself and won't call JavaScript functions (or vice versa) when it's time to remove it as you rely on third party software running outside your applications domain.

If you don't NEED the YouTube API, but merely a quick way to get a video within your application, the safest bet is to use an old style object embed for IE9 and the API / IFRAME embedding for the rest of the sane world.

<object width="{WIDTH}" height="{HEIGHT}">
<embed src="https://www.youtube.com/v/{VIDEO_ID}?version=3&autoplay=1"
type="application/x-shockwave-flash"
allowscriptaccess="always"
width="{WIDTH}" height="{HEIGHT}"></embed>
</object>

Removing above object (you can use SWFObject's "embedSWF" and "removeSWF" just fine for this btw) will get the video player off your page, without throwing any _flash_remove.. warnings.

If you NEED the YouTube API / control over the video player state:

Have you tried invoking the "destroy"-method on the ytplayer ? Unvoke the destroy and while I'm reluctant to posting "answers" using timeouts, give the Flash object some time to unregister BEFORE your set the source of the iframe to an empty string (so the document unloads), then clear the iframe or it's parent container.

Though I remember from a previous project this drove us mad (the context being a single-page interface where videos were dynamically added and removed) and we resorted to writing our own Flash player fallback using the AS3 YT code. That's how annoying it got.
[/quote]

解决办法,在 iframe 页面中加入如下代码:

// 解决 IE9 下, iframe 中flash不能正常销毁的问题
// see http://stackoverflow.com/a/18453540
$(window).unload(function() {
if (null != swfplayer) {
try {
swfplayer.stop2();
}catch(e){}
}

$("#player").remove();
$(window).remove();
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值