【问题标题】:Microsoft JScript runtime error: Unable to get value of the property 'removeChild': object is null or undefinedMicrosoft JScript 运行时错误:无法获取属性“removeChild”的值:对象为空或未定义
【发布时间】:2013-05-31 06:09:52
【问题描述】:

我正在开发一个 asp.net mvc 3 应用程序。标题告诉我有什么问题。我将描述我是如何得到这个错误的。

我正在使用 JavaScript 从我的剃刀视图上传图像。脚本不是很长,所以我会在这里发布:

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

通过使用此脚本,我在执行业务逻辑的控制器中获取了图像,因此我不确定是否真的是导致此问题的脚本,但在调试时我找不到其他原因。因此,当控制器完成其工作时,我会在 Visual Studio 2010 中打开一个名为 Script block[dynamic] 的新选项卡,并提供以下代码:

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

这实际上是我的 JavaScript 的一部分:

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

我对 JS 有非常基本的了解。我使用此代码,但我不理解其中的某些部分。在这里-通过评论和代码本身有点明显发生了什么,但是如果我对此发表评论并尝试上传图片,我不会收到错误,这是非常诱人的事情,但我认为代码很可能是好的,出现此错误的原因是在其他地方,所以我在这里发布以获取有关可能导致此错误的原因以及如何修复它的任何帮助?

附言

这是我用来上传图片的表格:

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

【问题讨论】:

    标签: c# javascript asp.net-mvc-3 razor


    【解决方案1】:

    这是 setTimeout 的语法

    setTimeout(function(),milliseconds);
    

    所以,你应该这样做

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

    【讨论】:

    • 好吧,我得到了和前面的代码完全相同的错误,但至少现在我可以在 VS2010 中进行某种调试,它表明 parentNode 实际上是 Null。我不太确定这在整个函数的上下文中意味着什么,但我在想是否可以执行某种检查,如if (iframeId != null) { //here the rest of the code; },或者必须有父节点,如果某处出现问题它是空的?
    • 您的 iframeId 在哪里...??它有父...??
    • 好吧,我认为这里设置了父级 - form.parentNode.appendChild(iframe);。要查看表单的外观,请检查我编辑的问题,因为我无法在此处发布。
    • form.parentNode.removeChild(iframeId); 也用同样的删除
    • 我收到Microsoft JScript runtime error: DOM Exception: NOT_FOUND_ERR (8) 但这似乎是合乎逻辑的。表单是父级。我需要从表单中删除节点。我猜这是表单的 childNode。
    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多