当 iframe 加载时,如何让 iframe 在主页上运行 Javascript 函数?
我有这样的 HTML:
<script type="text/javascript">
function uploadDone(response) {
alert(response);
}
function init() {
document.getElementById('file_upload_form').onsubmit=function() {
document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe
document.getElementById("upload_target").onload = uploadDone(response);
}
}
</script>
<form method="get" id="file_upload_form" enctype="multipart/form-data" action="../includes/parsecsv.ajax.php" target="upload_target">
<label class="uploadClick">
<button class="normal">Click to Upload Sign Ups CSV</button>
<input type="file" id="uploadSignups file" name="file">
</label>
<span class="uploadDrag"><span>or</span>Drag Your Sign Ups CSV Here</span>
<button type="submit" name="action">hello</button>
<iframe id="upload_target" name="upload_target" src="" style="width:0px; height:0px; border:none;"></iframe>
</form>
当您单击提交按钮时,应该通过 上传文件输入中选定的文件。我似乎已经能够让它进行上传,但上传完成后似乎没有运行
uploadDone()
?有什么想法吗?
I have this HTML:
<script type="text/javascript">
function uploadDone(response) {
alert(response);
}
function init() {
document.getElementById('file_upload_form').onsubmit=function() {
document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe
document.getElementById("upload_target").onload = uploadDone(response);
}
}
</script>
<form method="get" id="file_upload_form" enctype="multipart/form-data" action="../includes/parsecsv.ajax.php" target="upload_target">
<label class="uploadClick">
<button class="normal">Click to Upload Sign Ups CSV</button>
<input type="file" id="uploadSignups file" name="file">
</label>
<span class="uploadDrag"><span>or</span>Drag Your Sign Ups CSV Here</span>
<button type="submit" name="action">hello</button>
<iframe id="upload_target" name="upload_target" src="" style="width:0px; height:0px; border:none;"></iframe>
</form>
It is supposed to, when you click the submit button, upload the selected file in the file input ajaxically through an <iframe>
. I seem to have been able to get it to do the uploading, but it does not seem to run uploadDone()
when the uploading is complete? Any ideas what is wrong with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想您只需向 iFrame 的窗口元素添加一个事件即可:
另外,请注意我将
uploadDone
包装在一个函数内;否则,它只会调用它并将事件设置为它返回的任何内容。I guess you would just add an event to the iFrame's window element:
Also, notice I'm wrapping the
uploadDone
inside a function; otherwise, it would just call it and set the event as whatever it returns.使用 IFrame 的 Parent 属性,如何从 javascript 访问父 Iframe
Use the parent property of the IFrame, How to access parent Iframe from javascript
在这里,您需要创建一个 JSP 文件,其中包含响应和
在 JSP 中编写一些脚本来访问已加载到文档中的
uploadDone
方法。JSP 中的脚本可能类似于:
Here, you need to create a JSP file which will have the response and
write some script in JSP to access the
uploadDone
method, which is already loaded in document.The script in JSP can be something like: