无法在js弹出窗口中嵌入对象

时间:2012-01-24 18:14:03

标签: javascript jquery google-chrome

我有这段代码:

<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='800' height='470' id='Gen5Core' align='middle'><param name='allowScriptAccess' value='always'/><param name='allowFullScreen' value='true'/><param name='movie' value='http://h71016.www7.hp.com/html/interactive/_core/Gen5Core.swf?corePath=http://h71016.www7.hp.com/html/interactive/_core/&serverName=http://h71016.www7.hp.com/html/interactive/p1102w/'/><param name='quality' value='high'/><param name='bgcolor' value='#333333'/><embed src='http://h71016.www7.hp.com/html/interactive/_core/Gen5Core.swf?corePath=http://h71016.www7.hp.com/html/interactive/_core/&serverName=http://h71016.www7.hp.com/html/interactive/p1102w/' quality='high' bgcolor='#333333' width='800' height='470' name='Gen5Core' align='middle' allowScriptAccess='always' allowFullScreen='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>

如果我直接将它放入页面,它就完美无缺。

但我想要的是将它嵌入到弹出窗口中,所以我编写了这个函数:

<script type="text/javascript">
    $(document).ready(function () {
        $('.newWindow').click(function (event) {
            var termWin = window.open('', '', 'width=600,location=0,toolbar=0,resizable=1,height=400,menubar=0,scrollbars=1');
            termWin.document.write('<html><head><title>TITLE</title><body><div> <object classid=\'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\' width=\'800\' height=\'470\' id=\'Gen5Core\' align=\'middle\'><param name=\'allowScriptAccess\' value=\'always\'/><param name=\'allowFullScreen\' value=\'true\'/><param name=\'movie\' value=\'http://h71016.www7.hp.com/html/interactive/_core/Gen5Core.swf?corePath=http://h71016.www7.hp.com/html/interactive/_core/&serverName=http://h71016.www7.hp.com/html/interactive/p1102w/\'/><param name=\'quality\' value=\'high\'/><param name=\'bgcolor\' value=\'#333333\'/><embed src=\'http://h71016.www7.hp.com/html/interactive/_core/Gen5Core.swf?corePath=http://h71016.www7.hp.com/html/interactive/_core/&serverName=http://h71016.www7.hp.com/html/interactive/p1102w/\' quality=\'high\' bgcolor=\'#333333\' width=\'800\' height=\'470\' name=\'Gen5Core\' align=\'middle\' allowScriptAccess=\'always\' allowFullScreen=\'true\' type=\'application/x-shockwave-flash\' pluginspage=\'http://www.macromedia.com/go/getflashplayer\'></embed></object> </div></body></html>');
            termWin.document.close(); termWin.focus();
        });
    });
</script>

将在用户点击类newWindow的链接时打开。

 <a href="#" class="newWindow">
            <img src="@Url.Content("~/Content/Images/icon_demo_black.gif")" />
        </a>

我有一个问题:

  • Chrome 中,弹出窗口为空,因此嵌入对象无效(但我看到Inspect已将代码成功放入html源代码中)。

你能伸出援助之手吗?

1 个答案:

答案 0 :(得分:1)

在窗口准备好接受写入之前,Chrome正在触发其余的javascript。我现在没有太多时间在所有浏览器上测试它,但它适用于Chrome 18和IE9 http://jsfiddle.net/fordlover49/GP2Gz/

基本上,我们只需要一个固定的100毫秒延迟的setTimeout,以便让浏览器有时间准备好窗口。

请注意,原始答案(相同的主体)添加了对文档readyState的额外检查,但是readyState在浏览器中不一致(例如,Firefox 9的窗口状态为“未初始化”,直到你填写网页上的网址或其他内容。)

我对此进行了测试,它似乎在IE8&amp; IE9,Firefox 3.6.8&amp; 9,Chrome 17&amp; 18

$(document).ready(function() {
    var termWin;

    function writeWindow() {
        termWin.document.write('<html><head><title>TITLE</title><body><div> <object classid=\'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\' width=\'800\' height=\'470\' id=\'Gen5Core\' align=\'middle\'><param name=\'allowScriptAccess\' value=\'always\'/><param name=\'allowFullScreen\' value=\'true\'/><param name=\'movie\' value=\'http://h71016.www7.hp.com/html/interactive/_core/Gen5Core.swf?corePath=http://h71016.www7.hp.com/html/interactive/_core/&serverName=http://h71016.www7.hp.com/html/interactive/p1102w/\'/><param name=\'quality\' value=\'high\'/><param name=\'bgcolor\' value=\'#333333\'/><embed src=\'http://h71016.www7.hp.com/html/interactive/_core/Gen5Core.swf?corePath=http://h71016.www7.hp.com/html/interactive/_core/&serverName=http://h71016.www7.hp.com/html/interactive/p1102w/\' quality=\'high\' bgcolor=\'#333333\' width=\'800\' height=\'470\' name=\'Gen5Core\' align=\'middle\' allowScriptAccess=\'always\' allowFullScreen=\'true\' type=\'application/x-shockwave-flash\' pluginspage=\'http://www.macromedia.com/go/getflashplayer\'></embed></object> </div></body></html>');
        termWin.document.close();
        termWin.focus();
    }

    $('.newWindow').click(function(event) {
        termWin = window.open('', '', 'width=600,location=0,toolbar=0,resizable=1,height=500,width=840,menubar=0,scrollbars=1');
        setTimeout(writeWindow, 100)
    });
});