在JavaScript中测试变量是否未定义的最合适方法是什么?我见过几种可能的方法:
if (window.myVariable)
Run Code Online (Sandbox Code Playgroud)
要么
if (typeof(myVariable) != "undefined")
Run Code Online (Sandbox Code Playgroud)
要么
if (myVariable) //This throws an error if undefined. Should this be in Try/Catch?
Run Code Online (Sandbox Code Playgroud) 如何确定变量undefined还是null?我的代码如下:
var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
//DO SOMETHING
};
Run Code Online (Sandbox Code Playgroud)
<div id="esd-names">
<div id="name"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
但是如果我这样做,JavaScript解释器会暂停执行.
尝试在Google(或其他Google托管的lib)上加载托管jQuery的好方法,但如果Google尝试失败,请加载我的jQuery副本?
我不是说谷歌是片状的.有些情况下谷歌副本被封锁(例如在伊朗).
我会设置一个计时器并检查jQuery对象吗?
这两份副本的危险是什么?
并不是真的在寻找"只使用谷歌"或"只使用自己的"等答案.我理解这些论点.我也理解用户可能会缓存Google版本.我正在考虑一般的云回退.
编辑:这部分补充......
由于Google建议使用google.load加载ajax库,并在完成后执行回调,我想知道这是否是序列化此问题的关键.
我知道这听起来有点疯狂.我只想弄清楚它是否可以以可靠的方式完成.
更新:jQuery现在托管在微软的CDN上.
如何检查是否以跨浏览器的方式定义了JavaScript变量?
在使用FireBug日志记录编写一些JavaScript时遇到了这个问题.我写了一些代码如下:
function profileRun(f) {
// f: functions to be profiled
console.profile(f.constructor);
f();
console.profileEnd(f.constructor);
}
Run Code Online (Sandbox Code Playgroud)
它在FireFox/FireBug中工作正常,但它在IE8 RC1中报告错误.所以,我想检查执行环境中是否存在控制台变量.
下面的代码在FireFox中工作正常,但在IE8 RC1中没有.
function profileRun(f) {
if (console != undefined) {
console.profile(f.constructor);
}
f();
if (console != undefined) {
console.profileEnd(f.constructor);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做的话.它适用于IE8 RC1.为什么?
function profileRun(f) {
if (window.console != undefined) {
console.profile(f.constructor);
}
f();
if (window.console != undefined) {
console.profileEnd(f.constructor);
}
}
Run Code Online (Sandbox Code Playgroud)
是否有任何跨浏览器的方式来检查它?
我正在检查if(response[0].title !== undefined),但是我得到了错误:
未捕获的TypeError:无法读取未定义的属性"title".
如何查找变量是否未定义?
我目前有:
var page_name = $("#pageToEdit :selected").text();
var table_name = $("#pageToEdit :selected").val();
var optionResult = $("#pageToEditOptions :selected").val();
var string = "?z=z";
if ( page_name != 'undefined' ) { string += "&page_name=" + page_name; }
if ( table_name != 'undefined' ) { string += "&table_name=" + table_name; }
if ( optionResult != 'undefined' ) { string += "&optionResult=" + optionResult; }
Run Code Online (Sandbox Code Playgroud) 如何检查变量名称是否"正在使用"?含义:如何检查变量名是否已在var varname = 'something'语句中使用?
通常,我会检查是否typeof varname == 'undefined',这似乎工作正常.问题是当页面上有一个元素时id="varname".现在,当我检查时typeof varname == 'undefined',我得到了false,因为varname是一些HTML元素.
varname不是"在使用中",但它也不undefined是.
<body id="test1"></body>
<script>
//if <body> has an id of test1, how do i check if test1 is undeclared?
console.log(typeof test1 == 'undefined'); // the <body> element causes this to be true
console.log(typeof test2 == 'undefined'); // outputs true as expected
</script>
Run Code Online (Sandbox Code Playgroud)
另外,你能检查角落情况:var test1 = document.getElementById('test1')已经执行了吗?
在访问其errorMessage属性之前,检查jsonObject是否未定义的最短语法是什么?
var jsonObject = SomeMethodReturningAnObject();
if (jsonObject.errorMessage === undefined) // jsonObject can be undefined and this will throw an error
/* success! */
else
alert(jsonObject.errorMessage);
Run Code Online (Sandbox Code Playgroud) 在我的beforeSend功能中我有这个
$.myloader.show();
但是有些脚本没有这个元素所以我的这一行给了我错误
$.myloader.show(); 给我错误$ .myloader不存在
我该怎么办呢
if($.myloader exists)
$.myloader.show();
Run Code Online (Sandbox Code Playgroud) 我有2个javascript代码.除此之外,检查varable是否等于null或变量类型是否未定义.但在一种情况下,我得到错误,在其他情况下,我没有得到任何错误.
代码1:
if (NS1 === null || typeof (NS1) === 'undefined') {
... }
Run Code Online (Sandbox Code Playgroud)
代码2:
if (window.NS1 === null || typeof (window.NS1) === 'undefined') {
... }
Run Code Online (Sandbox Code Playgroud)
对于代码1,我收到错误
NS1未定义
而对于代码2,我没有得到任何错误.我不明白可能是什么原因,因为我没有定义NS1或window.NS1.所以我应该在这两种情况下都会出错.