无法在 IE 模式下从外部文件调用方法

Failed to call method from external file in IE Mode

提问人:Saurabh 提问时间:11/1/2023 最后编辑:Saurabh 更新时间:11/1/2023 访问量:21

问:

我有一个外部js文件,其中包含我在.aspx页面中调用的一些方法。它们在 edge、chrome、mozila 中都运行良好,但在 IE 模式下效果不佳

以下是我的代码结构

.aspx 页

<html>
<head>
<script type="text/javascript" language="javascript" src="../js/externalfile.js"></script>
<script language="javascript" type="text/javascript">
function PageValidate()
{
 if (!request_validator('form1')){
            return false;
        }
}
</script>
</head>
<body>
//some HTML Here
</body>
</html>

外部 JS 文件

function request_validator(formname)
{
    if (window.document.documentMode) {
        var alltxtbox = document.getElementById(formname).getElementsByTagName("input");
    }
    else {
        var alltxtbox = Array.from(document.forms).filter(x => x.id == formname)[0].getElementsByTagName("input");
    }
    if (alltxtbox != null)
    {
        for (i=0;i<alltxtbox.length;i++)
        {
            var inputstr=alltxtbox[i].value;
            if (inputstr!="")
            {
                var rr=/(<[A-Za-z!]{1})|(\&\#)/;
                gotit=rr.exec(inputstr);
                if (gotit!=null)
                {
                    alert("you input is not allowed,please input again!\r\n'&lt;' instead of '<';\r\n'&gt;' instead of '>';\r\n'&amp;' instead of '&';");
                    if (alltxtbox[i].disabled == false)
                        alltxtbox[i].focus();
                    return false;
                }   
            }
        }
    }
    //textarea
    //eval("var alltextarea=document." + formname + ".getElementsByTagName(\"textarea\");");
    if (window.document.documentMode) {
        var alltextarea = document.getElementById(formname).getElementsByTagName("textarea");
    }
    else {
        var alltextarea = Array.from(document.forms).filter(x => x.id == formname)[0].getElementsByTagName("textarea");
    }
    if (alltextarea != null)
    {
        for (i=0;i<alltextarea.length;i++)
        {
            var inputstr=alltextarea[i].value;
            if (inputstr!="")
            {
                var rr=/(<[A-Za-z!]{1})|(\&\#)/;
                gotit=rr.exec(inputstr);
                if (gotit!=null)
                {
                    alert("you input is not allowed,please input again!\r\n'&lt;' instead of '<';\r\n'&gt;' instead of '>';\r\n'&amp;' instead of '&';");
                    if (alltextarea[i].disabled == false)
                        alltextarea[i].focus();
                    return false;
                }   
            }
        }
    }
    return true;
}

当我在IE模式下运行它并调用这些js函数时,它会给出以下错误

属性“request_validator”的值为 null 或未定义,而不是 Function 对象

有人可以解释为什么来自外部文件的 js 方法在 IE 模式下没有被初始化吗?

更新 1

添加了 alert/log 语句来检查代码中断的位置

function PageValidate()
{
 alert('calling request_validator');
 if (!request_validator('form1')){
            return false;
        }
}

并在 request_validator 内添加了 alert('Inside request_validator');

运行代码时仅收到警报“调用request_validator”,这意味着它不会进入方法,在控制台中仅收到此错误

SCRIPT5007:属性“request_validator”的值为 null 或未定义,而不是 Function 对象

JavaScript HTML asp.net Web 表单

评论

1赞 Pointy 11/1/2023
在此之前还有其他错误吗?
0赞 T.J. Crowder 11/1/2023
上面没有跳出任何内容,除了元素上的属性已被弃用至少 24 年,并且如果存在类型,则始终被忽略,并且当代码是 JavaScript 时,该属性是不必要的(即使在 IE 中)。至少,我会删除已弃用的 attr,并且可能两者都删除。但我怀疑这真的是问题所在。我不认为我们可以仅根据上述内容提供帮助,我们需要更多的内容来继续前进。languagescripttype
0赞 Saurabh 11/1/2023
@T.J.Crowder 需要更多代码细节吗?
0赞 Saurabh 11/1/2023
添加了方法request_validator(formname)的完整代码实现,希望可能会有所帮助
1赞 T.J. Crowder 11/1/2023
@Saurabh - 好吧,当我这样做时它起作用,所以我恐怕帮不了你。也许您在某处有另一个具有该名称的函数?在松散模式下,全局范围内的重复函数声明不是错误,如果有错误,则以后者为准。(虽然这并不能解释为什么它在IE模式下工作。你有任何其他全球名称吗?或者它是否显示为 HTML 元素上的 OR?request_validatoridname

答: 暂无答案