0

I am creating a website which will only allow a user on it if your location is unblocked, I was trying to make if location doesn't exist, then don't allow the user

I tried using

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

and

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

and

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

But none of the above methods worked.

Here are my two functions

  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
    Where's the code containing the lines you are talking about? As far as I see, you have not defined a variable called `showPosition` – Nico Haase May 08 '19 at 14:06
  • @NicoHaase then it should say object is null if the variable is undefined – Das Shield May 08 '19 at 14:08
  • How would a function be undefined? You should be checking for `!!navigator.geolocation` to see if the browser engine supports it – epascarello May 08 '19 at 14:08
  • 1
    You defined function called `showPosition`, so it's `typeof showPosition === "function"` – ponury-kostek May 08 '19 at 14:09
  • 1
    Try `if ( showPosition === undefined )` with triple equals `===` – Conan May 08 '19 at 14:10
  • @ponury-kostek i want to know if the output of that function is nothing – Das Shield May 08 '19 at 14:10
  • 2
    The function does not return anything and it is called asynchronously so it make no sense. – epascarello May 08 '19 at 14:11
  • @DasShield then do `if(!showPosition())` – ponury-kostek May 08 '19 at 14:11
  • #1: Use the === operator instead of == because == performs an type conversation. #2: don't use innerHTML. There is the risk, that your innerHTML will not be correct in future HTML version. Also DOM manipulating methods are optimised. #3: you forget to call the showPosition with (). Youre only asking if the reference == null what it is not. – Kai Lehmann May 08 '19 at 14:11
  • 5
    All the comments about the check are useless since what OP is trying to do is not possible. The logic has to change for this to work. – epascarello May 08 '19 at 14:12
  • @ponury-kostek still doesn't work... – Das Shield May 08 '19 at 14:16
  • Possible duplicate of [check if location setting has been turned off in users browser](https://stackoverflow.com/questions/14862019/check-if-location-setting-has-been-turned-off-in-users-browser) – chevybow May 08 '19 at 14:17
  • @chevybow no, this is not a duplicate, i have looked for hours for an answer – Das Shield May 08 '19 at 14:19
  • 1
    @DasShield If it is not a duplicate then please explain in the body of your post why it is not rather than just saying you've looked at other questions for hours. – chevybow May 08 '19 at 14:20
  • @chevybow that is a completely different question to my one, why would i write in the body why it isn't that one, i am not asking this question specifically about location, I am asking this question about how to check if a function gives an answer or not – Das Shield May 08 '19 at 14:28
  • What do you mean by "a function gives an answer"? The given function `showPosition` does not return anything. Instead, it tries to access a variable `x` that has not been defined. – Nico Haase May 08 '19 at 15:12
  • @NicoHaase the variable x has been defined, just not shown in the code i put on... – Das Shield May 08 '19 at 20:33

3 Answers3

0

The way I understand your question is you want to allow access to users if only they have location capability on their browsers and have allowed access, here is a solution that might be of help:

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
}

I hope this solves your problem

And in future if you want to test if a variable has any value, just do:

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

if you just do:

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

This will return true if the variable has value 0 or is an empty string ('') inclusive of undefined and null.

lulliezy
  • 1,741
  • 5
  • 24
  • 49
  • if I do ```var test = "" if (typeof test === 'undefined' || test === null) {document.getElementById("demo").innerHTML = "object is null ";}``` , then it doesn't display anything, but if I do ```if (typeof test1 === 'undefined' || test1 === null) ``` then it says "object is null", so it doesn't seem to be working – Das Shield May 08 '19 at 20:01
  • yea, because when you assign the `test` variable to an empty string, its no longer `undefined`, its now `string`, now when you do `typeof test === 'string'` it will return true. This explains why the first condition fails and last condition passes because in the last scenario, you have not assigned anything to the variable `test1` hence its undefined – lulliezy May 09 '19 at 12:03
  • it should get categorised under null if it is an empty string – Das Shield May 09 '19 at 20:03
  • @DasShield null and empty string are two different very separate things, null means no value, empty string means there is a value which is a string and the value is empty string, go ahead and do `typeof null` and `typeof ''` and observe the output – lulliezy May 10 '19 at 07:10
  • please accept this answer if it has solved your problem – lulliezy May 10 '19 at 13:17
0

There seems to be a general lack of understanding of the geoLocation api. The mdn website will be helpful, but to get you started, try this snippet:

Here's a codepen version as stackoverflow blocks its snippets from accessing user location apparently (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>
junvar
  • 11,151
  • 2
  • 30
  • 46
  • Please add some explanation to your code such that the OP can learn from it – Nico Haase May 08 '19 at 14:49
  • there's not much I can add that isn't already on the wiki. A short snippet catered to their use case should be sufficient to get them on the right path – junvar May 08 '19 at 14:50
0

Try it with 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);
});
Conan
  • 319
  • 2
  • 9