当 iframe 加载时,如何让 iframe 在主页上运行 Javascript 函数?

发布于 2024-12-17 20:41:48 字数 1772 浏览 0 评论 0原文

我有这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∝单色的世界 2024-12-24 20:41:48

我想您只需向 iFrame 的窗口元素添加一个事件即可:

document.getElementById("upload_target").contentWindow.onload = function(response) { uploadDone(response) };

另外,请注意我将 uploadDone 包装在一个函数内;否则,它只会调用它并将事件设置为它返回的任何内容。

I guess you would just add an event to the iFrame's window element:

document.getElementById("upload_target").contentWindow.onload = function(response) { uploadDone(response) };

Also, notice I'm wrapping the uploadDone inside a function; otherwise, it would just call it and set the event as whatever it returns.

没有伤那来痛 2024-12-24 20:41:48

使用 IFrame 的 Parent 属性,如何从 javascript 访问父 Iframe

Use the parent property of the IFrame, How to access parent Iframe from javascript

过度放纵 2024-12-24 20:41:48

在这里,您需要创建一个 JSP 文件,其中包含响应和
在 JSP 中编写一些脚本来访问已加载到文档中的 uploadDone 方法。

JSP 中的脚本可能类似于:

 parent.document.uploadDone(response);

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:

 parent.document.uploadDone(response);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文