2010-06-09 15 views
0

아래 코드는 iframe을 동적으로 추가합니다.이 iframe에 자바 스크립트를 동적으로 삽입하는 방법이 있습니까?자바 스크립트 동적으로 iframe에 자바 스크립트 추가

var iFrame = document.createElement('iframe'); 
    var iFrameSRC = "http://www.google.com"; 
    iFrame.name = 'hehe'; 
    iFrame.id = 'hehe'; 
    iFrame.src = iFrameSRC; 
    iFrame.width = '1px'; 
    iFrame.height = '1px'; 
    iFrame.frameBorder = 0; 
    iFrame.style.overflow = "hidden"; 
    iFrame.style.align = "left"; 
    iFrame.style.display = "block"; 
    iFrame.style.fontSize = "10px"; 

    bodyInner.appendChild(iFrame); 

답변

-1

들으 아니, 당신은 다른 도메인의 문서에 코드를 삽입 할 수 없습니다. 이는 XSS (Cross-Site Scripting) 및 피싱 (Phishing) 공격을 막기 위해 브라우저에서 시행되는 보안 조치입니다.

2

는 나는 이런 식으로 뭔가 할 생각이가 iframe을 및 홀더 페이지가 같은 도메인에있는, 아니면 그냥 경우 수행 할 수 있습니다

var script=iframe.createElement('script'); 

    script=iframe.standardCreateElement('script'); 
    script.setAttribute('src','http://localhost/jquery-1.3.2.js'); 
    script.setAttribute('type','text/javascript'); 
    iframe.lastChild.firstChild.appendChild(script); 

주 당신은 북마크 또는 브라우저에서이를 호출하는 경우 확장/플러그인.

2

이 작동합니다 ... 아마 당신은이 같은 것을 만들려고 :

<body onload="initialize()"> 
<script language="JavaScript"> 
function initialize() { 
    var testFrame = 
     document.createElement("IFRAME"); 
    testFrame.id = "testFrame"; 
    testFrame.name = 'hehe'; 
    testFrame.src = "http://www.seekload.tk"; 
    testFrame.style.border='20px'; 
    testFrame.style.width='900px'; 
    testFrame.style.height='300px'; 
    testFrame.style.frameBorder = 10; 
    testFrame.style.overflow = "hidden"; 
    testFrame.style.align = "left"; 
    testFrame.style.display = "block"; 
    testFrame.style.fontSize = "10px"; 
    document.body.appendChild(testFrame); 
} 
</script> 

PS를 : 당신은 당신의 대신 위의 시도 코딩 몇 가지 실수를했다.

관련 문제