不得不说这个问题困扰了我很久,百度、google了一大把,想起了那句话:“有问题找百度,百度不行就谷歌,谷歌再找不到就自杀。”
中文搜索了一大通还是找不到答案,改成英文的,很快找到问题所在,看来以后要多变换一下思维,不要在惯性思维的树上吊死了。
原文如下:
Submitting a form with target set to a script-generated iframe on IE 写道
Although AJAX techniques are now widespread among the web-developers community, there’s still something that can’t be done using pure AJAX techiques (whatever that means): file uploads. There’s no way to grab a file using JavaScript and send it to the server using an asynchronous request. I guess the main reason for this is security, you don’t want web sites to steal files from your home directory.
Anyway, people found a relatively popular way to do this. The ideia is having a hidden iframe on the page, and use that iframe as a target of the form where you have the reference to the file to upload. So, when the form submits, the result page will go to the hidden iframe, and the user won’t see a full page refresh. You still have to take care of some details, like hiding the form and showing a nice progress bar with the classic “Hold on, we are uploading” message, and polling for the content of the iframe to check is the file is still in its way, or if it arrived safely.
One nice detail is where do you actually have the hidden iframe. You could just put the iframe on the HTML code of your page, but IMO that’s ugly. The iframe is an artifact that is needed just to serve as a black hole for the form submittion result page. It doesn’t make sense to put it in your HTML code, as it hasn’t anything to do with the content. Also, it’s error prone: you may inadvertently delete it, causing the file upload to missbehave. Or you may need to change the iframe code and having it on HTML will force you to update on all the pages where you use it (and of course, you will forget one).
So, I believe the most elegant solution (if you can use the word “elegance” in the context of this major hack) is to generate the iframe using javascript. You may do this on the page load, when you create the form, whatever. Just do it before the user submits the file! :) Well, it’s easy, right? Something like this does the trick:
var objBody = document.getElementsByTagName("body").item(0);
var iframe = document.createElement('iframe');
iframe.id = 'fileUploaderEmptyHole';
iframe.name = 'fileUploaderEmptyHole';
iframe.width = 0;
iframe.height = 0;
iframe.marginHeight = 0;
iframe.marginWidth = 0;
objBody.insertBefore(iframe, objBody.firstChild);
That’s cool, right? Just run this and the iframe will be created. Submit the form, the result goes in the hidden iframe, everything works, we are done, let’s go home. Well… all true until you actually test it on Internet Explorer…
If you test this on IE, the result will be a new window being created when you submit, which is clearly not what you want. Well, I had a hard time finding the problem, so here goes: if you create the iframe using JavaScript, IE wont set it’s name. Yes, the “iframe.name = ‘fileUploaderEmptyHole’;” line will simply be ignored. It does nothing. So, as you don’t have any frame called ‘fileUploaderEmptyHole’, when you submit the form, it will create a new window named ‘fileUploaderEmptyHole’ and display the result on it.
The solution it to hack this even further. Specifically:
iframe = document.createElement('<iframe name="fileUploaderEmptyHole">');
Yeah! Now you’re thinking “WTF?”. Yes, yes, it’s true. This actually works on IE, with the expected (?) results. Well, you still have to support the other browsers, but you are lucky, as this will throw an exception on all the non-IE browsers. So, it’s just a matter of catching it, and running the decent version of the code:
var iframe;
try {
iframe = document.createElement('<iframe name="fileUploaderEmptyHole">');
} catch (ex) {
iframe = document.createElement('iframe');
}
iframe.id = 'fileUploaderEmptyHole';
iframe.name = 'fileUploaderEmptyHole';
iframe.width = 0;
iframe.height = 0;
iframe.marginHeight = 0;
iframe.marginWidth = 0;
This is why I love the Web. Or maybe not.
关键是这句话:
If you test this on IE, the result will be a new window being created when you submit, which is clearly not what you want. Well, I had a hard time finding the problem, so here goes: if you create the iframe using JavaScript, IE wont set it’s name. Yes, the “iframe.name = ‘fileUploaderEmptyHole’;” line will simply be ignored. It does nothing. So, as you don’t have any frame called ‘fileUploaderEmptyHole’, when you submit the form, it will create a new window named ‘fileUploaderEmptyHole’ and display the result on it.
问题所在:在IE下,通过JS创建一个iframe object,对其name属性的赋值操作将被忽略。
解决方式:在IE和其他browser下可以通过以下方式创建iframe
var iframe;
try { // for I.E.
iframe = document.createElement('<iframe name="fileUploaderEmptyHole">');
} catch (ex) { //for other browsers, an exception will be thrown
iframe = document.createElement('iframe');
}
英文很容易读,剩下的就自己动手解决吧。
分享到:
相关推荐
js控制/获取a.html中iframe加载的b.html中的iframe,很多人都以为getElementById可以直接获取,其实不行的,以上代码就解决了这个问题,js控制iframe加载页面的iframe,不过测试只在IE和火狐通过,谷歌不行,其他自...
标题中的“弹出新窗口时用自己的窗口代替IE窗口”指的是在VB(Visual Basic)编程中,如何在打开新的浏览器窗口时,使用自定义的窗体(Form)而不是默认的Internet Explorer(IE)窗口来显示内容。这通常涉及到...
我最近也遇到了这样的问题,所以就把弹出新窗口的方法分享给大家。欢迎大家补充哦… 第一种、使用原生javascript的window.open()方法(大部分情况下会被浏览自阻止) 第二种、模拟表单(form)提交,原理是指定表单的...
在网页开发中,"from表单新窗口打开并提交"是一种常见的用户交互方式,它允许用户在新的浏览器窗口中打开一个表单,并在用户填写完信息后提交表单。这种方式通常用于创建更友好的用户体验,比如创建注册或登录页面时...
在使用Xilium CefGlue时,可能会遇到一个常见的问题,即浏览器组件在点击链接或执行JavaScript的`window.open`时,会弹出新的窗口,而不是在当前窗口或者在已存在的标签页中打开。这可能不符合用户的期望,特别是...
在IT领域,特别是前端开发中,JavaScript(简称JS)被广泛用于实现网页的动态功能,包括页面跳转和对iframe内的页面进行跳转与刷新。本文将深入解析JS页面跳转的各种方法,以及如何利用JS操作iframe进行页面跳转和...
在Web开发中,JavaScript(JS)的同源策略限制了不同源之间进行交互,包括从一个页面中的脚本访问另一个不同源的IFrame内容。"解决JS跨域访问IFrame的解决方案"这一主题关注的就是如何克服这个限制,使得在JSP页面中...
3. 创建一个`<form>`元素,将用户选择的文件作为`<input type="file">`的一部分,将`form`的`target`属性设置为刚才创建的IFrame的ID,这样表单提交就会在IFrame内进行。 4. 使用JavaScript触发`form`的`submit`...
这种方式简单易懂,但是需要注意的是,由于浏览器的安全策略限制,如果`iframe`与父页面不在同一个域下,则可能会因为同源策略而无法执行该操作。 ##### 方法二:使用HTML表单的`target`属性 这种方法适用于`iframe...
有时候我们需要从父页面修改`iframe`内元素的值,比如将一个文本框的值从一个`iframe`传递到另一个`iframe`: ```javascript // 将值赋给iframe中的TextBox window.frames["ifrMapCompanyDetails"].document.all(...
本文将深入探讨如何使用IFrame来实现AJAX的文件上传功能,这在某些场景下是必要的,比如处理IE浏览器的兼容性问题或者上传大文件时。 首先,让我们理解为什么需要IFrame来处理文件上传。传统的AJAX文件上传在现代...
`FormValidation.js` 是一个强大且灵活的JavaScript库,专门用于前端表单验证。它提供了丰富的校验规则、样式定制以及对动态添加元素的验证支持,使得开发者能够轻松地创建高效、用户体验良好的表单验证系统。本文将...
- 在设计时需考虑用户体验,避免频繁弹出新窗口导致的困扰。 3. **安全性**: - 使用客户端脚本时需要注意安全问题,防止XSS攻击等。 综上所述,通过结合`Response.Redirect`与客户端脚本技术,我们可以实现多种...
用iframe提交表单,主要是将表单提交到iframe中,可实现无刷新提交。 html页面: <iframe name="FORMSUBMIT" width="1" height="1" ></iframe> <form action="login....
首先在父页面的javascript给定义一个window.name,并赋予一个字符串值,如window.name="test",其中赋予的字符串值可以随意定义,然后在Iframe页面的Form 标签中定义target属性,并且其值也必须与之前定义的window....
在Web开发中,ExtJS是一个强大的JavaScript库,用于构建富客户端应用程序。在处理复杂的用户界面时,Panel和IFrame的组合使用是常见的场景。当Panel包含一个IFrame时,可能会遇到页面高度调整的问题,特别是在动态...
本篇文章将针对JavaScript弹出窗口的相关问题进行总结,包括无提示刷新网页、刷新页面的方法、弹出窗口代码、模式窗口的数据刷新问题、模式窗口中链接弹出新窗口以及无提示关闭页面的方法。 1. **无提示刷新网页** ...
例如,当用户在`Form1`上的一个按钮(假设为`Button1`)被点击时,会弹出一个新的窗口`Form2`。接下来的问题是:如何实现在`Form2`中的某个操作能够反过来影响`Form1`中的某些控件状态或行为?这个问题在多窗体应用...
图片上传通常涉及表单提交,我们可以创建一个包含文件输入字段的表单,并将其设置为`iframe`的`target`,这样表单提交就不会导致整个页面刷新。例如: ```html <form action="upload....