Window.location.href '不起作用
window.location.href isn't working
我在javascript上使用这个脚本来改变位置,并在url中获得一些变量,但window.location。href不工作,我无法重定向到另一个页面
在一些搜索中,我发现我需要把返回false;
我放了但是我没有任何结果
R: No error message appear
如何解决?
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>
看了你的评论,我觉得你想要这个
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>
window.location.href = "facee.php?lat="+lat+"long="+long;
应该是window.location.href = "facee.php?lat="+lat+"&long="+long;
也许你错过了一个&
不要过度拥挤你的代码函数。如何解决重定向问题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;
如果您的代码正常工作,这将"打印"在页面中的地理位置,并立即重定向您。用户永远不会看到result
打印。
3:
return false;
为什么?
使用前定义函数。它在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>
修改代码
window.location.href = "facee.php?lat="+lat+"long="+long;
:
document.location= "facee.php?lat="+lat+"&long="+long;
相关文章:
- Windows.Open 在 window.Location 工作时不起作用
- Javascript-将变量传递给php页面,window.location.href不起作用
- 使用 Div 标签的 location.href 属性时,Eval 不起作用
- 如果应用程序托管在IIS上,则Javascript window.location.htm不起作用
- Javascript document.location.reload不起作用
- 使用self.location.href重定向不会'不起作用
- top.window.location在IE中不起作用
- window.location.assign(varString)不起作用
- 检查window.location.href.indexOf是否'不起作用
- window.location.href 在 Meteor 中不起作用
- JavaScript window.location.href 不起作用
- window.location.href 不起作用,无法重定向
- Window.location 在 Internet Explorer 中不起作用
- 为什么ajax不起作用,除非我刷新或使用location.href
- window.location.href 在 Typescript 中不起作用
- $location.path() 在 facebook 登录后不起作用
- JavaScript Location.replace 不起作用
- Javascript window.location 在 MVC3 中不起作用
- AngularJS和UI路由器:为什么$state.go()和$location.path()在交互式控制台中不起作用
- window.location 在 javascript 中不起作用