`
陈修恒
  • 浏览: 199691 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

 
阅读更多
引:http://stackoverflow.com/a/18453540

引用

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.


解决办法,在 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();
});
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics