我已经确定了问题,但不确定到底是什么导致了问题。我的生产环境平等地处理 undefined 和 null (undefined == null
),这是所需的行为。该问题出现在 IE8 中,因为我的开发环境以不同方式处理 null 和 undefined (undefined != null
)。
我在下面提供了一段代码,它在两种环境中都是相同的。调用渲染函数时,没有为两个环境传递任何参数。我添加了控制台语句来显示每个环境如何处理传递到渲染函数的 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
在所有浏览器中均受支持,并将返回元素的文本类型或“未定义”。
关于javascript - IE8 JavaScript null != 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22970954/