2009-08-20 6 views
27

iframe 내부에로드 된 페이지를 언로드 할 수 있습니까? 가능한 경우 iframe src를 빈 페이지로 변경하고 싶지 않습니다. 나는 기본적으로 코드가 이전에로드 된 페이지를 지우지 않는 것을 제외하고는 $('#frameID').attr("src","");과 같은 것을 할 무언가를 찾고있다.iFrame에서 콘텐츠 언로드/제거

내부에로드 된 내용이 없도록 iframe을 재설정 할 수있는 "unload" 함수가 있나요?

+4

새 iframe을 하나만 삭제하고 만들면됩니다. –

+5

'$ ("# frameID"). attr ("src", "")'Firefox 11에서 나에게 잘 작동합니다. 빈 iframe을 원한다면'src'를' about : 빈 문자열 대신 빈. –

+0

@IlmariKaronen'about : blank'는 이상하게 작동합니다 !!! – TheVillageIdiot

답변

24

다른 솔루션은 innerHTML을 사용합니다. 이는 XHTML에서 항상 작동하지는 않습니다. 또한 document.body 만 지 웁니다 (<head>에있는 항목이 아직 존재 함).

var frame = document.getElementById("myFrame"), 
frameDoc = frame.contentDocument || frame.contentWindow.document; 
frameDoc.documentElement.innerHTML = ""; 
+1

덕분에 내가 이걸 사용 했어. – Dan

+1

좋은 해결책, 정말 고마워 !! –

+2

[AJ Ostergaard] (http://stackoverflow.com/users/63306/aj-ostergaard)는 [제안 수정] (http://stackoverflow.com/suggested-edits/227353)에서 지적했듯이 iframe이 기본 페이지와 동일한 도메인에있는 경우 작동합니다. 나는 잘못 배치 된 것에 대한 편집을 거부했지만 그의 요점은 여전히 ​​정확하고 언급할만한 가치가있다. –

4

$ ('# frameID'). contentWindow.document.body.innerHTML = '';

동일한 iframe과 마찬가지로, 동일한 도메인에있는 경우에만 작동합니다.

+1

당신은'.contentWindow'를 할 수 있습니까? – geowa4

1

첫째, 프레임의 문서를 얻을 : 다음

var frame = $('#frameId').get(0); 
var frameDoc = frame.contentDocument || frame.contentWindow.document; 

을 빈은 :

나는이 너무 일을해야한다고 생각
frameDoc.getElementsByTagName('body')[0].innerHTML = ""; 

:

$('body', frameDoc).html(""); 

을 지금, 당신은 수도 머리에로드 될 수있는 스크립트로 무언가를하고 싶지만, 시작해야합니다.

2
$("#frameId").contents().find("div#SomeDIVinsideFrame").remove(); // removes some div content inside iframe 

$("#FrameId").remove(); // removes frame 

당신은의 언로드 이벤트를 트리거 할 수 있습니다 http://www.livepage.info

1

에 iframe이 뉴스를 보여 같은 문제가 없었다 :

var frame = document.getElementById("myFrame"), 
frameDoc = frame.contentDocument || frame.contentWindow.document; 
frameDoc.removeChild(frameDoc.documentElement); 

이 솔루션은 innerHTML 사용 다음은 DOM을 사용하는 솔루션입니다 그 iframe like

$('body').trigger('unload'); 

그런 다음 부모 창에서 iframe을 제거하고 필요한 경우 새 iframe에 새 iframe을 다시로드하십시오.

$('#iframe_wrapper').html(''); 
$('#iframe_wrapper').html('<iframe src="...">'); 
0
function getContentFromIframe(iFrameName) 
{ 

var myIFrame = document.getElementById(iFrameName); 
var content = myIFrame.contentWindow.document.body.innerHTML; 

//Do whatever you need with the content  

} 

에게 확실히 !!!!!!!!!!!!!!!! 작동합니다

7

이 시도

$("iframe").contents().find("body").html(''); 

그것은 단지 내부 태그의 innerHTML을 클리어하고 모든 브라우저에서 아주 간단하고 작업을 다시로드하지 않고 iframe을 재사용 할 수 있도록 실제로 iframe을 언로드하지!

+3

iframe이 동일한 도메인에서로드 된 경우에만 작동합니다. – Robert

+0

예, 다른 도메인에서 작업 할 수 없습니다 !!! –

2

iframe을 제거하고 다시 만드는 것이 가장 현명한 해결책입니다 (다른 답변에 대한 공격은 아닙니다).에만 저장이 변수를 플러시하지 않는 iframe 대응의 innerHTML

이 조심 바운드 이벤트 리스너 등, 그것은 메모리 누수, 여러 트리거 같은 문제 (가 많이 발생할 수 있습니다 제거함으로써

같은 이벤트 등).

0

이것은 나를 위해 일했으며 iframe 태그 내의 모든 항목을 지 웠습니다. 몸, 머리, HTML과 모든 :

$("iframe").contents().empty(); 
4

당신이 동적으로 iframe을, 하나 개의 쓰기에서 다른 누출로드 된 모든 스크립트/변수의 내용을 생성 합니다. 따라서 DOM 엘리먼트 을 삭제하는 @Eli가 제공하는 솔루션은에서 작동하지 않습니다. 한마디로

:

는 div 요소로 iframe을 포장하고 DOM의 컨텐츠를 교체, 청소합니다.

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>JS Bin</title> 
</head> 
<body> 
    <div id="wrapper"> 
    <iframe id="test"></iframe> 
    </div> 
</body> 
</html> 

청소하려면 세부 정보

var wrapper = document.getElementById('wrapper'); 
wrapper.innerHTML= "<iframe id='test'></iframe>"; 

을 :

var iframe = document.getElementById('test'); 

// define variable 'a' 
var content = "<html><body><script>var a=555;</script></body></html>"; 

iframe.contentWindow.document.open(); 
iframe.contentWindow.document.write(content); 
iframe.contentWindow.document.close(); 

// uncomment this to clean the iframe 
//document.getElementById('wrapper').innerHTML= "<iframe id='test'></iframe>"; 

// write 'a' if defined 
var content2 = "<html><body><div id='content'></div><script>document.getElementById('content').innerHTML=typeof a === 'undefined' ? 'undefined' : a;</script></body></html>"; 

var iframe2 = document.getElementById('test'); 
iframe2.contentWindow.document.open(); 
iframe2.contentWindow.document.write(content2); 
iframe2.contentWindow.document.close(); 
두 iframe을 사이에 스크립트 누설의 예 (크롬 테스트) 글을

스크립트 누설의 데모

이 코드를 실행하면 다음 코드가 출력됩니다. 두 번째 iframe은 첫 번째 iframe에서 정의되었지만 555입니다.

중간 부분의 주석을 제거하면 예상대로 작동합니다.

관련 질문 : Avoiding memory leaks loading content into an iframe

1
var frame = document.getElementById("myframe"); 
frame.src = "about:blank"; 

이 나를에서 근무하고 방지 메모리 누수도. 제 경우에는 부모도 역시 파괴해야했습니다. 이 경우 메모리 누출을 막기 위해 약간의 지연 시간을두고 부모를 삭제해야합니다.

+0

'부모'란 부모 페이지 또는 IFRAME 태그가 포함 된 DOM의 상위 요소를 의미합니까? – donohoe

+0

iframe이 포함 된 부모 요소입니다. – Antonis