[Javascript] iframe innerHTML
|
var x = document.getElementsByTagName(“iframe”)[0]; var y = null; if (x.contentWindow != null) { y = x.contentWindow; } else if (x.contentDocument != null) { y = x.contentDocument; } if (y != null && y.document != null) { y = y.document; } y.getElementsByTagName(“html”)[0].innerHTML = “contents”; |
위의 코드를 조금 수정해서, 아이프레임의 document 를 가져오는 코드 함수화
|
function getFrameDocument(_iframeId) { var frameObj = document.getElementById(_iframeId); var frameDoc = null; if (frameObj.contentWindow != null) { frameDoc = frameObj.contentWindow; } else if (frameObj.contentDocument != null) { frameDoc = frameObj.contentDocument; }
if (frameDoc != null && frameDoc.document != null) { frameDoc = frameDoc.document; }
return frameDoc; } |
이어서 html 태그 내의 내용을 innerHTML 함수로 가져오기
|
var frameDoc = getFrameDocument(“draftActionFrame”);
if (frameDoc != null && frameDoc.getElementsByTagName(“html”) != null) { alert(frameDoc.getElementsByTagName(“html”)[0].innerHTML); } |