1

动态创建iframe在IE下的两个问题

 2 years ago
source link: https://blogread.cn/it/article/6849?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

表单提交到动态创建的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>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK