mhmd mhmd - 1 month ago 8
Javascript Question

window.location.href isn't working

I'm using this script on javascript to change location and get some variable in the url but window.location.href didn't work i can't redirect to another page

during some search i found i need to put return false;

i put it but also i don't have any result

R: No error message appear

How to solve that ?

function getPosition() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successPosition);
} else {
document.getElementById("result").innerHTML = "Your browser does not support geo location api"
}

function successPosition(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
document.getElementById("result").innerHTML = "Latitude:" + lat + "<br/>Longitude:" + long
window.location.href = "facee.php?lat=" + lat + "&long=" + long;
}
return false;
}


<form method="POST" action="" enctype="multipart/form-data" onsubmit="return getPosition();">
<input type="file" name="image" accept="image/*" capture="camera" class="filestyle" data-buttonBefore="true" data-iconName="glyphicon glyphicon-camera" data-buttonText="Take apicture." />
</div>
<div class="col-md-12"></div>
<div class="col-md-4">
<input type="submit" name="Add" value="Add" class="btn btn-success">
</form>



Answer

After reading your comments, I THINK you want this

function successPosition(position) {
  var lat = position.coords.latitude,
    long = position.coords.longitude,
    form = document.getElementById("myForm");
  document.getElementById("result").innerHTML = "Submitting Latitude:" + lat + "<br/>Longitude:" + long;
  form.lat.value = lat;
  form.long.value = long;
  form.submit(); // HERE we submit
};

function getPosition() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successPosition);
  } else {
    document.getElementById("result").innerHTML = "Your browser does not support geo location api ";
  }
  return false; // always block submission
}

using

<form id="myForm" method="POST" action="facee.php" 
enctype="multipart/form-data" onsubmit="return getPosition();">
  <input type="hidden" name="lat" value="" />
  <input type="hidden" name="long" value="" />
  <input type="file" name="image" accept="image/*" capture="camera" class="filestyle" data-buttonBefore="true" data-iconName="glyphicon glyphicon-camera" data-buttonText="Take apicture." />
  </div>
  <div class="col-md-12"></div>
  <div class="col-md-4">
    <input type="submit" name="Add" value="Add" class="btn btn-success">
</form>
<span id="result"></span>