2013-04-20 2 views
0

У меня есть следующий сценарий для создания IFRAMEНевозможно вызвать метод «AppendChild» нуля для фреймов

function createIframe() { 
    var iframe = document.createElement("iframe"); 
    iframe.style.position = "absolute"; 
    iframe.style.visibility = "hidden"; 

    document.body.appendChild(iframe); 

    // Firefox, Opera 
    if(iframe.contentDocument) iframe.doc = iframe.contentDocument; 
    // Internet Explorer 
    else if(iframe.contentWindow) iframe.doc = iframe.contentWindow.document; 

    // Magic: Force creation of the body (which is null by default in IE). 
    // Also force the styles of visited/not-visted links. 
    iframe.doc.open(); 
    iframe.doc.write('<style>'); 
    iframe.doc.write("a{color: #000000; display:none;}"); 
    iframe.doc.write("a:visited {color: #FF0000; display:inline;}");  
    iframe.doc.write('</style>'); 
    iframe.doc.close(); 

    // Return the iframe: iframe.doc contains the iframe. 
    return iframe; 
    } 

однако в хромированной консоли он дает мне ошибку не удается вызвать метод «AppendChild» нулевой

Почему он не работает?

+0

Когда вы получите сообщение об ошибке? Я запустил его на своей консоли Chrome, это сработало! Просто проверьте, '' document.createElement ("iframe"); 'возвращает элемент! – NINCOMPOOP

ответ

0

Код верный. Вы добавили тело на страницу?

Попробуйте это:

<html> 
<body> 
<script> 
window.onload=function() { 
    createIframe() 
}; 
function createIframe() { 
    var iframe = document.createElement("iframe"); 
    iframe.style.position = "absolute"; 
    iframe.style.visibility = "hidden"; 

    document.body.appendChild(iframe); 

    // Firefox, Opera 
    if(iframe.contentDocument) iframe.doc = iframe.contentDocument; 
    // Internet Explorer 
    else if(iframe.contentWindow) iframe.doc = iframe.contentWindow.document; 

    // Magic: Force creation of the body (which is null by default in IE). 
    // Also force the styles of visited/not-visted links. 
    iframe.doc.open(); 
    iframe.doc.write('<style>'); 
    iframe.doc.write("a{color: #000000; display:none;}"); 
    iframe.doc.write("a:visited {color: #FF0000; display:inline;}");  
    iframe.doc.write('</style>'); 
    iframe.doc.close(); 

    // Return the iframe: iframe.doc contains the iframe. 
    return iframe; 
    } 
</script> 
</body> 
</html> 

iframe.doc.write не чистое решение ....

Смежные вопросы