简体   繁体   English

Microsoft JScript运行时错误:无法获取属性'removeChild'的值:对象为null或未定义

[英]Microsoft JScript runtime error: Unable to get value of the property 'removeChild': object is null or undefined

I'm working on an asp.net mvc 3 application. 我正在开发一个asp.net mvc 3应用程序。 The title tells what problem I have. 标题告诉我有什么问题。 I will describe how I get this error. 我将描述如何得到此错误。

I'm using JavaScript to upload image from my razor view. 我正在使用JavaScript从剃刀视图上传图像。 The script is not that long so I'll post it here : 该脚本不是很长,所以我将其发布在这里:

function fileUpload(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)', 250);
        }

        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");

        // Submit the form...
        form.submit();

        document.getElementById(div_id).innerHTML = "Uploading...";
        form.setAttribute("action", '/forms/displayform');
    }

By using this script I get the image in my controller where I do my business logic so I'm not sure if it's really the script that is causing this, but I can't find another reason while debugging. 通过使用此脚本,我可以在控制器中执行业务逻辑的位置获得映像,因此我不确定是不是导致此问题的脚本,但是在调试时找不到其他原因。 So when the controller finish its work I get a new tab open in the visual studio 2010 titled Script block[dynamic] and the following code is there : 因此,当控制器完成工作时,我在Visual Studio 2010中打开了一个名为“ Script block[dynamic] ”的新选项卡,其中包含以下代码:

function anonymous()
{
iframeId.parentNode.removeChild(iframeId)
}

which is actually part of my JavaScript : 这实际上是我的JavaScript的一部分:

    // Del the iframe...
    setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);

I have very basic knowledge about JS. 我对JS非常了解。 I use this code but I don't understand some parts of it. 我使用此代码,但我不理解其中的某些部分。 Here - by the comment and the code itself is somewhat obvious what's going on but if I comment this and try to upload an image I don't get error and this is something very tempting to do, but I think that most probably the code is OK and the reason to get this error is somewhere else, so I post here to get any help on what may cause this error and how can it be fixed? 在这里-通过注释和代码本身可以明显看出正在发生的事情,但是如果我对此进行注释并尝试上传图像,我不会出错,这是很诱人的事情,但是我认为代码很可能是好的,发生此错误的原因在其他地方,因此我在此处发布以获取有关可能导致此错误的信息以及如何解决此问题的帮助。

PS 聚苯乙烯

And this is the form that I use for uploading the image: 这是我用来上传图片的表格:

@using (Html.BeginForm("Upload", "Forms", FormMethod.Post))
{
    <input name=@Model[0].DocumentId type="hidden" />

    <input type="file" name="datafile" id="file" onchange="readURL(this);" />
    <input type="button" name="Button" value="Upload Image" id="UploadButton" onclick="fileUpload(this.form,'/forms/upload','uploadImg'); return false;"/>
    <div id="uploadImg" style="display: inline-block;">
        <img id="blah" src="#" alt="your image" style="display:none;"/>
    </div>
}

This is the syntax for setTimeout 这是setTimeout的语法

setTimeout(function(),milliseconds);

So , you should do something like this 所以,你应该做这样的事情

setTimeout(function(){
           iframeId.parentNode.removeChild(iframeId);
           }, 250);

暂无
暂无

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

相关问题 Microsoft JScript运行时错误:无法获取属性“样式”的值:对象为null或未定义 - Microsoft JScript runtime error: Unable to get value of the property 'style': object is null or undefined Microsoft JScript运行时错误:“null”为null或不是对象 - Microsoft JScript runtime error: 'null' is null or not an object asp.net jscript错误:无法获取未定义或空引用的属性“值” - asp.net jscript Error: Unable to get property 'value' of undefined or null reference 无法获取属性“ length”的值:对象为null或未定义-Javascript错误 - Unable to get value of the property 'length': object is null or undefined - Javascript Error Microsoft JScript运行时错误:“ data.EmployeeDetails.EmployeeId”为null或不是对象` - Microsoft JScript runtime error: 'data.EmployeeDetails.EmployeeId' is null or not an object` Microsoft JScript 运行时错误:'Sys.Extended.UI' 是 null 或者不是 object - Microsoft JScript runtime error: 'Sys.Extended.UI' is null or not an object Microsoft JScript运行时错误:对象不支持此属性或方法 - Microsoft JScript runtime error: Object doesn't support this property or method Microsoft JScript运行时错误:对象不支持此属性或方法 - Microsoft JScript runtime error: Object doesn't support this property or method Microsoft JScript运行时错误:“ clientUploadComplete”未定义 - Microsoft JScript runtime error: 'clientUploadComplete' is undefined JavaScript运行时错误:无法获取未定义或空引用的属性“msie” - JavaScript runtime error: Unable to get property 'msie' of undefined or null reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM