window.location.href不起作用

时间:2016-10-28 07:44:50

标签: javascript php

我在javascript上使用此脚本更改位置并在网址中获取一些变量,但 window.location.href 无法正常工作我无法重定向到其他网页

在某些搜索中,我发现我需要将返回false;

我把它但我也没有任何结果

R:没有出现错误消息

如何解决?

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>

5 个答案:

答案 0 :(得分:0)

window.location.href = "facee.php?lat="+lat+"long="+long; 应该 window.location.href = "facee.php?lat="+lat+"&long="+long; 也许你错过了&#39;&amp;&#39;。

答案 1 :(得分:0)

不要过度拥挤你的代码功能。 如何解决重定向问题 window.location.href = "/facee.php?lat="+lat+"&long="+long;

但是,这不是代码中唯一的问题。 你正在做很多不对的事情

1

function getPosition() {
//...
    function successPosition(position) {

每次致电getPosition时,您都在重新定义sucessPosition

2

document.getElementById("result").innerHTML = "Latitude:" + lat + "<br/>Longitude:" + long
window.location.href = "facee.php?lat=" + lat + "long=" + long;

如果您的代码正常运行,则会打印出#34; print&#34;页面中的地理位置并立即重定向您。用户永远不会看到result打印。

3:

return false;

为什么?

答案 2 :(得分:0)

阅读完评论后,我想你想要这个

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
}
使用

<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>

答案 3 :(得分:-1)

使用前定义功能。 它在https://jsfiddle.net/jerrysin/q0hck8as/

上工作正常
<script>
  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;
  };

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

</script>

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

答案 4 :(得分:-2)

更改您的代码

window.location.href = "facee.php?lat="+lat+"long="+long; 

到此:

document.location= "facee.php?lat="+lat+"&long="+long;