繁体   English   中英

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

[英]How do you make an iframe run a Javascript function on the main page when the iframe loads?

我有这个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>

当您单击“提交”按钮时,应该将所选文件通过<iframe>随意地上传到文件输入中。 我似乎已经能够使它执行上载,但是上载完成后似乎没有运行uploadDone() 任何想法有什么问题吗?

我想您只是将事件添加到iFrame的window元素中:

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

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

使用iframe的parent属性, 如何从javascript访问父iframe

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

JSP中的脚本可以是这样的:

 parent.document.uploadDone(response);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM