为什么form.submit()时失败IE9,当形式iframe和用户通过Gmail来(Why fo

2019-10-16 16:02发布

我创建函数来发送电子邮件与反向链接到我的网站,我使用笨框架

之后用户点击电子邮件中的特定链接(反向链接),用户重定向到我的网页,其中有一个iframe。

我用iframe来提交与文件输入形式,进行页面清爽。

当用户使用IE9浏览器的Gmail通过链接来form.submit()函数失败,在其他浏览器它正常工作和其他电子邮件(Gmail的除外)了。

恳求帮助我找到解决方案

谢谢。

更新

其实我使用ajaxupload jQuery库,它失败就行了form.submit(); 在上述情况下

     /* Creates form, that will be submitted to iframe
     * @param {Element} iframe Where to submit
     * @return {Element} form
     */
    _createForm: function(iframe){
        var settings = this._settings;

        // We can't use the following code in IE6
        // var form = document.createElement('form');
        // form.setAttribute('method', 'post');
        // form.setAttribute('enctype', 'multipart/form-data');
        // Because in this case file won't be attached to request                    
        var form = toElement('<form method="post" enctype="multipart/form-data"></form>');

        form.setAttribute('action', settings.action);
        form.setAttribute('target', iframe.name);                                   
        form.style.display = 'none';
        document.body.appendChild(form);

        // Create hidden input element for each data key
        for (var prop in settings.data) {
            if (settings.data.hasOwnProperty(prop)){
                var el = document.createElement("input");
                el.setAttribute('type', 'hidden');
                el.setAttribute('name', prop);
                el.setAttribute('value', settings.data[prop]);
                form.appendChild(el);
            }
        }
        return form;
    },
    /**
     * Gets response from iframe and fires onComplete event when ready
     * @param iframe
     * @param file Filename to use in onComplete callback 
     */
    _getResponse : function(iframe, file){            
        // getting response
        var toDeleteFlag = false, self = this, settings = this._settings;   

        addEvent(iframe, 'load', function(){                

            if (// For Safari 
                iframe.src == "javascript:'%3Chtml%3E%3C/html%3E';" ||
                // For FF, IE
                iframe.src == "javascript:'<html></html>';"){                                                                        
                    // First time around, do not delete.
                    // We reload to blank page, so that reloading main page
                    // does not re-submit the post.

                    if (toDeleteFlag) {
                        // Fix busy state in FF3
                        setTimeout(function(){
                            removeNode(iframe);
                        }, 0);
                    }

                    return;
            }

            var doc = iframe.contentDocument ? iframe.contentDocument : window.frames[iframe.id].document;

            // fixing Opera 9.26,10.00
            if (doc.readyState && doc.readyState != 'complete') {
               // Opera fires load event multiple times
               // Even when the DOM is not ready yet
               // this fix should not affect other browsers
               return;
            }

            // fixing Opera 9.64
            if (doc.body && doc.body.innerHTML == "false") {
                // In Opera 9.64 event was fired second time
                // when body.innerHTML changed from false 
                // to server response approx. after 1 sec
                return;
            }

            var response;

            if (doc.XMLDocument) {
                // response is a xml document Internet Explorer property
                response = doc.XMLDocument;
            } else if (doc.body){
                // response is html document or plain text
                response = doc.body.innerHTML;

                if (settings.responseType && settings.responseType.toLowerCase() == 'json') {
                    // If the document was sent as 'application/javascript' or
                    // 'text/javascript', then the browser wraps the text in a <pre>
                    // tag and performs html encoding on the contents.  In this case,
                    // we need to pull the original text content from the text node's
                    // nodeValue property to retrieve the unmangled content.
                    // Note that IE6 only understands text/html
                    if (doc.body.firstChild && doc.body.firstChild.nodeName.toUpperCase() == 'PRE') {
                        doc.normalize();
                        response = doc.body.firstChild.firstChild.nodeValue;
                    }

                    if (response) {
                        response = eval("(" + response + ")");
                    } else {
                        response = {};
                    }
                }
            } else {
                // response is a xml document
                response = doc;
            }

            settings.onComplete.call(self, file, response);

            // Reload blank page, so that reloading main page
            // does not re-submit the post. Also, remember to
            // delete the frame
            toDeleteFlag = true;

            // Fix IE mixed content issue
            iframe.src = "javascript:'<html></html>';";
        });            
    },        
    /**
     * Upload file contained in this._input
     */
    submit: function(){                        
        var self = this, settings = this._settings;

        if ( ! this._input || this._input.value === ''){                
            return;                
        }

        var file = fileFromPath(this._input.value);

        // user returned false to cancel upload
        if (false === settings.onSubmit.call(this, file, getExt(file))){
            this._clearInput();                
            return;
        }

        // sending request    
        var iframe = this._createIframe();
        var form = this._createForm(iframe);

        // assuming following structure
        // div -> input type='file'
        removeNode(this._input.parentNode);            
        removeClass(self._button, self._settings.hoverClass);
        removeClass(self._button, self._settings.focusClass);

        form.appendChild(this._input);

        form.submit();

        // request set, clean up                
        removeNode(form); form = null;                          
        removeNode(this._input); this._input = null;            

        // Get response from iframe and fire onComplete event when ready
        this._getResponse(iframe, file);            

        // get ready for next request            
        this._createInput();
    }
};

Answer 1:

检查代码的iframe中,如果未指定在iframe form.submit()

document.iframename.form.submit();


文章来源: Why form.submit() is fails IE9 , when form in iframe and user coming through gmail