如何检测变量是否包含任何内容

时间:2019-05-08 14:05:17

标签: javascript

我正在创建一个网站,该网站仅在您的位置被解除阻止时才允许用户访问,我正尝试确定如果不存在该位置,则不允许该用户访问

我尝试使用

if (typeof(showPosition)=="undefined") {
   document.getElementById("x").innerHTML = "object is null ";
}

if (!showPosition){
  document.getElementById("x").innerHTML = "object is null ";
}

if (showPosition == "null"){
  document.getElementById("x").innerHTML = "object is null ";
}

但是上述方法均无效。

这是我的两个功能

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition, showError);
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;

3 个答案:

答案 0 :(得分:0)

我理解您的问题的方式是,如果用户只有在他们的浏览器中具有定位功能并允许访问,则希望允许他们访问,这是一个可能有所帮助的解决方案:

if (!navigator.geolocation) {
    // Location does not exist, do what you wanna do

    // Always remember to exit your functions early
    return;
}

// Location exists, proceed to ask for location permissions
navigator.geolocation.getCurrentPosition(showPosition, showError);

// Function to call when permission granted
function showPosition(location) {
    x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}

// Function to call when permission denied
function showError(error) {
    // error has code and message; goto: https://developer.mozilla.org/en-US/docs/Web/API/PositionError
}

我希望这可以解决您的问题

将来,如果您要测试变量是否具有任何值,请执行以下操作:

if (typeof variable === 'undefined' || variable === null) {
    // variable is either undefined or null, so basically does not contain anything
}

如果您只是这样做:

if (!variable) {
    // variable is either undefined or null, so basically does not contain anything
}

如果变量的值为0或为包含''undefined的空字符串(null),则返回true。

答案 1 :(得分:0)

似乎普遍缺乏对geoLocation API的理解。 mdn website会有所帮助,但要开始使用,请尝试以下代码段:

这是一个Codepen版本,因为stackoverflow显然阻止了其片段访问用户位置(https://codepen.io/anon/pen/VOvrwb

const NOT_SUPPORTED_BY_BROWSER = 0, SUPPORTED_BUT_BLOCKED = 1, SUPPORTED_AND_ALLOWED = 2;

let getGeoLocationStatus = () => {
  if (!navigator.geolocation)
    return Promise.reject(NOT_SUPPORTED_BY_BROWSER);

  return new Promise((resolve, reject) =>
      navigator.geolocation.getCurrentPosition(
          position => resolve(position),
          () => reject(SUPPORTED_BUT_BLOCKED)))
};

let x = document.getElementById("x");

getGeoLocationStatus().then(position => {
  x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}).catch(reason => {
  x.innerHTML = reason === NOT_SUPPORTED_BY_BROWSER ? 'Geolocation is not supported by this browser.' : 'blocked by user';
});
<div id="x"></div>

答案 2 :(得分:0)

尝试Promise:

//this is the function you wanna test it returns
function showPosition(){
    //Do some staff
    return 1;
}
//Creating a promise 
var promise = new Promise(function(resolve, reject) {
  try{
    let sp = showPosition();
    if( sp !== undefined && sp !== null ){
        resolve(sp);
    }else{
        reject(err);
    }
  }catch{
    reject(err);
  }
});

promise.then((res)=>{ 
    console.log('response of function is '+res);
}).catch((err)=>{
    console.debug('erreur'+err);
});