IE8 Javascript null != undefined

IE8 Javascript null != undefined

本文关键字:undefined null Javascript IE8      更新时间:2023-09-26

我已经确定了问题,但不确定是什么导致了问题。我的生产环境正在平等地处理未定义和null(undefined == null),这是所需的行为。这个问题发生在IE8中,因为我的开发环境对null和undefined的处理方式不同(undefined != null)。

我在下面提供了一段代码,这段代码在两种环境中都是相同的。调用render函数时,不会为两个环境传递任何参数。我添加了控制台语句,以显示每个环境如何处理传递到呈现函数中的args

var PortalView = (function() {
    return new function() {
    $.extend(true, this, SystemController);
        var self = this;
        //Render: 'args' is not defined in this case causing the issue
        self.render = function(args){
            if (args == null){                     console.dir('null!!');}
            if (args == undefined){                console.dir('undefined!!');}
            if (args != undefined && args != null){console.dir('NEITHER');}
            if (args == null && args == undefined){console.dir('BOTH!!');}
            console.dir('args:'+args);      
            var args = (args != undefined) ? args : {};
            args.el = (args.el != undefined) ? args.el : '#content';  //ERROR OCCURS HERE (line 21)
            args.template = "PortalTemplate"; //HTML template
            self.renderTemplate(args);        //inherited from 'SystemController'
        };
    }
})();

以下是控制台结果:

Production Environment (desired results):
   LOG: "null!!" 
   LOG: "undefined!!" 
   LOG: "BOTH!!" 
   LOG: "args:undefined" 
Development Environment (undesired results)
   LOG: "null!!" 
   LOG: "args:undefined" 
   SCRIPT5007: Unable to get value of the property 'el': object is null or undefined 
   vwaPortalView.js, line 21 character 4

我正在寻找会导致这种错误发生的场景。如有任何提示,我们将不胜感激。

编辑我已查明问题的真正根源。它是由对跨站点(提供身份验证服务的内部intranet站点)的JQUERY Ajax请求引起的。jsonpCallback参数破坏了一切。如果有人知道什么可以省去我的麻烦。。。太好了,否则我想从现在开始我应该能够处理它!我提供了ajax调用作为问题来源的参考(在IE8中)

        $.ajax({
                url: 'http://xyz.aspx', //changed for obvious reasons
                dataType: 'jsonp',
                jsonp: 'callback',
                error: function (request, status, error) {console.dir(error);}, //Parse error
                jsonpCallback: function(data, status){
                    console.dir(data);
                    console.dir(status);
                }
            });

使用类型

var args = (typeof args === "undefined") ? {} : args;

所有浏览器都支持typeof,它将返回元素的文本类型或"未定义"。