微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 无法阻止iframe加载

所以,我正在修补一个非常拥挤的网络电台播放器,遗憾的是一切都必须在生产模式下打补丁.

<div class="hidden_element" id="hideflash">
    <iframe frameBorder="0" width="315" height="45" id="flash" scrolling="no" src="THESOURCE"></iframe>
</div>
<script type="text/javascript">
    var firefox = /Firefox/i.test(navigator.userAgent);
    if (Modernizr.flash && !firefox) {
        // Supports Flash and is not Firefox
        $("#html5").addClass("is-splash");
        $("#html5").addClass("is-paused");
        $('video').removeAttr("autoplay");
        $("#seekbar").hide();
        $("#html5").hide();
    } 
    if (firefox) {
        // Firefox Detected- normal HTML5 Initiation
        document.getElementById("flash").contentDocument.close();
        window.frames[0].stop();
        window.frames[0].document.execCommand('Stop');
        $("#hideflash").hide();
    }
    if (!Modernizr.flash) {
        // Flash is not supported
        document.getElementById("flash").contentDocument.close();
        window.frames[0].stop();
        window.frames[0].document.execCommand('Stop');
        $("#hideflash").hide();
    }
</script>

我使用Modernizr自定义脚本来确定浏览器是Firefox还是支持Flash.如果这一切都不是真的,我会卸载HTML5播放器并按计划启动flash iframe.但是,如果检测到Firefox或缺少Flash支持,我会隐藏iframe并启动HTML5播放器.

这是一个非常有问题的设置,但这是满足我公司需求的唯一方法.主要问题是用于停止加载iframe的代码有时会起作用,有时却不起作用.

对这个棘手的建议有什么建议吗?

解决方法:

正如帕特里克所说,你可以使用闪存作为后备.通过使用视频标签,您可以加快网站(而非视频)的加载时间.您可以使用以下内容

<div class="hidden_element" id="hideflash">
    <iframe frameBorder="0" width="315" height="45" id="flash" scrolling="no" src="THESOURCE"></iframe>
</div>
<video src="video.ogv" controls>
    <script type="text/javascript">
        // Flash is not supported
        document.getElementById("flash").contentDocument.close();
        window.frames[0].stop();
        window.frames[0].document.execCommand('Stop');
        $("#hideflash").hide();
    </script>
</video>

您需要对此进行测试并根据需要进行调整.
有关更多信息,请使用this,thisthis网站,根据您的需要,每个网站都有不同的方式.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐