使用iframe而不是createpopup不起作用?

时间:2015-05-10 09:07:41

标签: javascript html iframe

我使用iframe代替window.createPopup,但它在控制台中给出了一个错误,例如“oDucument is not defiend”。

这是我的代码:

function ShowDropdown(oMenu, oMenuDropDown){
//Set popup html
var iframe='<iframe class="adframe" id="111" name="widget" src="#" width="300" height="250" marginheight="0" marginwidth="0" frameborder="no" scrolling="no"></iframe>';
var oframe = document.createElement('iframe');
    var oDocument = oframe.document;
oDocument.open();
var sHTML = "<html><head><link rel='stylesheet' type='text/css' href='../style.css'></head><body topmargin=0 rightmargin=0 leftmargin=0 bottommargin=0 style='overflow:visible;border-width:0px;'>"
            + oMenuDropDown.innerHTML + "</body></html>";
oDocument.write(sHTML);
oDocument.close();

//show popup
iframe.show(0, 0, 207, 50);}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

你在这里做了一些非常随意的事情。

var iframe='<iframe class="adframe" id="111" name="widget" src="#" width="300" height="250" marginheight="0" marginwidth="0" frameborder="no" scrolling="no"></iframe>';

您现在有一个名为&#34; iframe&#34;的字符串,其中包含一些HTML 但除非在调用

时,否则永远不会使用该字符串
iframe.show(0, 0, 207, 50);

这很可能会给你一个错误,因为字符串没有.show()函数。

与您创建iframe并行:

var oframe = document.createElement('iframe');

分配一些内容:

var oDocument = oframe.document;
oDocument.open();
var sHTML = "<html><head><link rel='stylesheet' type='text/css' href='../style.css'></head><body topmargin=0 rightmargin=0 leftmargin=0 bottommargin=0 style='overflow:visible;border-width:0px;'>"
        + oMenuDropDown.innerHTML + "</body></html>";
oDocument.write(sHTML);
oDocument.close();

但那就是它,你什么也做不了。

如果你得到的错误确实是oDucument,那么它必须在你在此处显示的代码之外,因为根本没有oDucument
此外,oDocumentundefined,因为oframe.document不存在,它是oframe.contentWindow.document,但即便如此,只有在添加iframe后才能使用function ShowDropdown(oMenu, oMenuDropDown) { // Construct the element purely in JS: var iframe = document.createElement('iframe'); iframe.className = 'adframe'; iframe.id = '111'; iframe.name = 'widget'; iframe.width = 300; iframe.height = 250; iframe.marginWidth = 0; iframe.marginHeight = 0; iframe.frameBorder = 0; iframe.scrolling = 'no'; // Set the position: iframe.style.position = 'absolute'; iframe.style.top = '50px'; // Change this value to what you need iframe.style.left = '300px'; // Change this value to what you need // Set the contents: var sHTML = '<html><head><link rel="stylesheet" type="text/css" href="../style.css"></head><body topmargin="0" rightmargin="0" leftmargin="0" bottommargin="0" style="overflow:visible;border-width:0px">' + oMenuDropDown.innerHTML + '</body></html>'; iframe.src = 'data:text/html;charset=UTF-8,' + encodeURI(sHTML); // Show popup: document.body.appendChild(iframe); } 到DOM树并完成加载。

更简单的解决方案是使用data: URI(请参阅this answer):

oMenu

另外,请考虑删除{{1}}参数,因为您没有使用它。

答案 1 :(得分:0)

iframe元素没有show方法,因此以这种方式无法实现您可能尝试的方法。 您应该将对话框内容置于定位div并显示该内容。

对于错误消息,iframe没有文档属性,因此返回null。在空值上调用open会产生错误。