因iframe问题

时间:2012-09-14 09:11:21

标签: javascript jsp iframe

我有一个jsp形式的两个单选按钮(遍历和直接)。正常提交表单的Traverse和使用iframe的表格的直接表格。使用iframe提交表单后,如果我尝试正常提交,应用程序是在新窗口中打开。如何防止在新窗口中打开表单。

function submitAction() {       
        var fileValue = document.getElementById("file1").value;     
        if (fileValue == "") {
            alert("Please uploaded file type of .war or .html type");
            return false;

        }           
        var valuePassed = getCheckedRadio();
        var url="uploadfile.do?rad=" + valuePassed;     
        if (valuePassed == "traverse") {                    
            document.EntryForm.action = url; 
            document.EntryForm.submit();    //Normal way of submitting.                         
        }else if (valuePassed == "direct") {
            alert("direct");    
            url=url+"&fileName="+fileValue;             
            directConvert(document.EntryForm,url,'downloaddiv');                }
    }



function directConvert(form, action_url, div_id){               
        // Create the iframe...
        var iframe = document.createElement("iframe");
        iframe.setAttribute("id", "upload_iframe");     
        iframe.setAttribute("name", "upload_iframe");
        iframe.setAttribute("width", "0");
        iframe.setAttribute("height", "0");
        iframe.setAttribute("border", "0");
        iframe.setAttribute("style", "width: 0; height: 0; border: none;");
        // Add to document...
        form.parentNode.appendChild(iframe);
        //window.frames['upload_iframe'].name = "upload_iframe";     
        iframeId = document.getElementById("upload_iframe");     
        // Add event...

        var eventHandler = function () {     
                if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
                else iframeId.removeEventListener("load", eventHandler, false);             
                // Message from server...    

                if (iframeId.contentDocument) {                
                    content = iframeId.contentDocument.body.innerHTML;
                } else if (iframeId.contentWindow) {                    
                    content = iframeId.contentWindow.document.body.innerHTML;
                } else if (iframeId.document) {                 
                    content = iframeId.document.body.innerHTML;
                }               
                document.getElementById(div_id).innerHTML = content;     
                // Del the iframe...
                setTimeout('iframeId.parentNode.removeChild(iframeId)',50);
            }

        if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
        if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);  

        // Set properties of form...
        form.setAttribute("target", "upload_iframe");
        form.setAttribute("action", action_url);
        form.setAttribute("method", "post");
        form.setAttribute("enctype", "multipart/form-data");
        form.setAttribute("encoding", "multipart/form-data");  
        form.submit();              
    }

当我第一次尝试遍历它正常工作但通过iframe提交后,如果我尝试遍历它将在新窗口中打开。如何阻止它在新窗口中打开。

1 个答案:

答案 0 :(得分:0)

因为您正在将form EntryForm对象传递给iframe,并在进行form.setAttribute("target", "upload_iframe");提交时更改directConvert

因此即使您尝试通过form提交,traverse目标仍然存在,但由于iframe已从页面中删除(setTimeout('iframeId.parentNode.removeChild(iframeId)',50);)提交后的窗口而不是iframe

因此,您可以在target提交中更改_selftraversedocument.EntryForm.target = "_self"

顺便说一句,{4.0}中的target属性为deprecated