这个问题(或类似问题)似乎经常被问到,但我尝试了很多方法来检查从我拥有的函数返回的值是否为空;无济于事。
我的函数,按名称获取 URL 参数:
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
但是,显然参数可能不存在,所以我需要检查返回的值。
做console.log( getURLParameter('client') );
退货null
......但做空检查不起作用。
我尝试了以下方法:
if ( getURLParameter("client") !== null ) {
alert("It's there matey!");
}
if ( getURLParameter("client") != null ) {
alert("It's there matey!");
}
if ( ! getURLParameter("client") ) {
alert("It's there matey!");
}
这些似乎都不适合我。
我有什么地方出错了吗?我可以在 vanilla JS 中或使用 jQuery 库中执行此操作。