问题描述:

在使用iframe方式异步上传文件时,ie7对name属性的处理有些不同,如果采用先创建iframe,再使用setAttribute方法添加name属性的方式,form提交数据返回时会找不到iframe,从而会新打开一个页面。

ie7下会失败的例子:

var iframe = document.createElement('iframe');
iframe.setAttribute('id', 'IframeNode');
iframe.setAttribute('name', 'IframeNode');
iframe.style.display = 'none';
document.body.appendChild(iframe);


iframe.onload = function(){
    document.getElementById('Result').innerHTML = 
        iframe.contentWindow.document.body.innerHTML;
}

var form = document.getElementById('Form');
form.target = 'IframeNode',
form.action = '/php/create_iframe.php';

form.submit();

 

有两种方法可以解决这个问题。

首先,不能使用setAttribute方法来设置name属性;

然后,

1. 用这种方式创建iframe

var iframe = document.createElement('<iframe name="IframeNode"></iframe>');

这种方式ie9以下可用。

 

2. 创建完iframe之后通过这种方式添加name属性

iframe.contentWindow.name = 'IframeNode';

这种方法所有浏览器都可用。

参考:http://stackoverflow.com/questions/2105815/weird-behaviour-of-iframe-name-attribute-set-by-jquery-in-ie

 

相关文章:

  • 2021-10-22
  • 2021-08-07
  • 2021-05-26
  • 2022-02-24
  • 2022-01-09
  • 2022-12-23
  • 2021-12-06
  • 2022-01-11
猜你喜欢
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2021-06-08
相关资源
相似解决方案