阻止在新窗口中打开iframe

时间:2012-09-13 11:49:02

标签: javascript iframe

我正在使用iFrame上传JSPForm。代码如下,

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();      
    }

表单正在正确提交但之后如果有任何操作,例如检查页面中的单选按钮导致在新窗口中打开应用程序。如何阻止它在新窗口中打开?

由于

1 个答案:

答案 0 :(得分:0)

不确定为什么单选按钮会导致任何问题,但如果表单正在提交,则问题可能是“目标”形式不再存在。

form.setAttribute("target", "upload_iframe");

从load事件处理程序中的DOM中删除“upload_iframe”元素。删除iframe后,尝试将目标设置为“_self”。

表单目标文档:http://www.w3schools.com/tags/att_form_target.asp

相关问题