2011-11-26 35 views
0

我有这样的HTML:iframe加载时,如何让iframe在主页上运行Javascript函数?

<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> 

它应该是,当你点击提交按钮,上传文件输入选定的文件ajaxically通过。我似乎已经能够得到它进行上传,但上传完成后似乎没有运行uploadDone()?任何想法它有什么不对?

+0

表包含ENCTYPE =的multipart/form-data的,但不包含的method = post。通常使用method = GET提交并且不使用enctype。 - 什么时候你的uploadDone()函数触发 – manny

回答

0

我想你想补充一个事件的iFrame窗口元素:

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

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

0

在这里,你需要创建这将对响应和 写在JSP一些脚本访问uploadDone方法,它已经加载文件JSP文件。

在JSP脚本可以是这样的:

parent.document.uploadDone(response); 
+2

请不要在SO中使用SMS发言('Here,u need');拼写完整的话。 –