0

I'm trying to redirect a user with window.location based on geolocation latitude and longitude coordinates. I only want the visitor to have access to my page when they're lat and long = 36.24, 76.21. I'm currently only checking against latitude, but will fix that later. Anyways, its not redirecting to the proper page. I can throw any value in the numlat variable and it will still redirect to work.php while it should redirect to didntwork.php if it doesn't match 36.24. Any ideas on what I'm doing wrong? Thanks!

Here's my current code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Your Address:</title>
<script type="text/javascript">
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude;
lang = position.coords.longitude;

var numlat=lat.toFixed(2);


if(numlat=36.24)
{
window.location.href = "work.php";
}
else
{
window.location.href = "didntwork.php";
}


});
</script

Thanks for the answer and explanation. I thought I tried the double == last night but it was getting late. Changing this worked!

2
  • = is for assignment, == is for comparison. Commented Nov 20, 2013 at 14:55
  • Numlat=36.24 isn't comparing them. Use == or === Commented Nov 20, 2013 at 14:55

2 Answers 2

1

if(numlat=36.24) should be:

if(numlat == '36.24')

it is both a string, and requires the double equals comparison.

Sign up to request clarification or add additional context in comments.

Comments

0
if(numlat=36.24) 

is setting the variable numlat to be 36.24 it will always evaluate to true. you want

if(numlat == '36.24'){...

as numlat becomes a string after .toFixed(2) is called

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.