IE 에서 동적 으로 iframe 을 만 드 는 두 가지 문제

2842 단어 JavaScript
태그:   iframe
동적 생 성 iframe 에 폼 제출
문제 설명
다음 코드 는 현재 페이지 에서 제출 폼 을 새로 고치 지 않 고 제출 하 는 데 사 용 됩 니 다. 그 원 리 는 폼 의 target 을 페이지 의 한 iframe id 로 설정 하여 이 iframe 을 제출 하 는 목표 로 삼 아 새로 창 을 열거 나 뛰 지 않도록 하 는 것 입 니 다.그러나 이 코드 는 IE 6, 7 에서 유효 하지 않 습 니 다.
<form action="http://www.baidu.com/" method="post" target="testframe">
	<input type="submit" value="Submit" />
</form>
<script>
var iframe = document.createElement('iframe');
iframe.id = 'testframe';
iframe.name = 'testframe';
document.body.insertBefore(iframe, document.body.firstChild);
</script>

해결 방안
innerHTML 방식 으로 iframe 을 삽입 합 니 다. 예 를 들 어:
<script>
var div = document.createElement('div');
div.innerHTML = '<iframe id="testframe" name="testframe" />';
document.body.insertBefore(div, document.body.firstChild);
</script>

동적 으로 iframe 을 만 드 는 onload 이벤트
문제 설명
다음 코드 는 페이지 에 iframe 을 만 들 고 iframe 에 onload 이벤트 리 셋 을 연결 합 니 다. 그러나 이 리 셋 은 IE 6, 7, 8 에서 모두 유효 하지 않 습 니 다.
<script>
var iframe = document.createElement('iframe');
iframe.id = 'testframe';
iframe.name = 'testframe';
iframe.src = 'http://www.baidu.com/';
iframe.onload = function() { alert('onload'); };
document.body.insertBefore(iframe, document.body.firstChild);
</script>

해결 방안
attachEvent 인터페이스 로 이벤트 리 셋 을 연결 합 니 다. 예 를 들 어:
<script>
var iframe = document.createElement('iframe');
iframe.id = 'testframe';
iframe.name = 'testframe';
iframe.src = 'http://www.baidu.com/';
var fn = function() { alert('onload'); };
if (iframe.attachEvent) {
	iframe.attachEvent('onload', fn);
} else {
	iframe.onload = fn;
}
document.body.insertBefore(iframe, document.body.firstChild);
</script>

다음 글 에 도 관심 이 있 을 수 있 습 니 다.
  • ie 아래 iframe 입력 상자 초점 분실 솔 루 션 [2012 - 09 - 20 13: 53: 30]
  • 크로스 도 메 인 수정 iframe 내 문자 [2012 - 02 - 05 15: 32: 23]
  • Google + 개발 팀 경험 공유 [2011 - 12 - 18 22: 24: 10]
  • iframe 자체 적응 고도 코드 [2011 - 08 - 23 13: 24: 07]
  • IFrame 이 가 져 온 세 션 문제 [2011 - 06 - 01 23: 34: 12]
  • 3 담 Iframe 자체 적응 고도 [2010 - 07 - 28 09: 36: 32]
  • 도 메 인 을 넘 어 요청 한 iframe 솔 루 션 (2) [2010 - 07 - 27 23: 29: 57]
  • 도 메 인 을 넘 어 요청 한 iframe 솔 루 션 (1) [2010 - 07 - 27 23: 29: 03]
  • JS 조작 iframe 의 dom [2010 - 07 - 23 00: 08: 26]
  • document. domain 과 iframe 을 사용 하여 역 내 AJAX 크로스 도 메 인 실현 [2010 - 07 - 21 09: 42: 37]
  • BO 보고서 시스템 에 포 함 된 Iframe 이 fireforx 에서 의 오류 수정 [2010 - 06 - 01 21: 56: 50]
  • iframe 리 src = "about: blank" 의 문제.[2010-06-01 13:11:24]
  • 좋은 웹페이지 즐겨찾기