无法在其他页面(HTML、Javascript)中重定向输出
我想找到当前位置的纬度和经度,然后点击按钮在另一页上打印出来。 虽然我成功地获得了纬度和经度,但无法在另一页上打印/重定向 有人能帮忙吗。请查找以下代码:无法在其他页面(HTML、Javascript)中重定向输出,javascript,html,google-maps,geolocation,Javascript,Html,Google Maps,Geolocation,我想找到当前位置的纬度和经度,然后点击按钮在另一页上打印出来。 虽然我成功地获得了纬度和经度,但无法在另一页上打印/重定向 有人能帮忙吗。请查找以下代码: <!DOCTYPE html> <html> <head> <script> <p id="demo"></p> var x = document.getElementById("demo"); function getLocation() { if (navi
<!DOCTYPE html>
<html>
<head>
<script>
<p id="demo"></p>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</head>
<body>
<h2 >Geo-Logical Application</h2><br>
<p>Click the button to get Longitude and Latitude for your current position.</p><br>
<form action="Listing.html">
<button type="submit" onclick="getLocation()">Submit</button>
</form>
</body>
</html>
var x=document.getElementById(“演示”);
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
功能显示位置(位置){
x、 innerHTML=“纬度:”+position.coords.Latitude+
“
经度:”+position.coords.Longitude;
}
地理应用
单击按钮获取当前位置的经度和纬度。
提交
我想将输出重定向到清单页面。
谢谢首先,脚本中不应该有p元素。它应该在身体里。另外,脚本标记应该位于主体的末尾,因为它包含修改DOM的代码。showPosition(position)函数应该在getLocation()函数之前,因为您在getLocation中使用showPosition。
- 使用
重定向到其他页面&window.location
- 将
传递到事件
以停止默认提交:getLocation
function getLocation(event) {
event.preventDefault();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(p){
showPosition(p);
//
window.location="Listing.html?lat="+p.coords.latitude+"&lgt="+p.coords.longitude;
});
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
HTML变成:
<form action="Listing.html">
<button type="submit" onclick="getLocation(event)">Submit</button>
</form>
否则,请使用sessionStorage
创建有状态的应用程序:
sessionStorage.setItem('POS',JSON.stringify(position.coords))
在Listing.html
中,通过以下方式获取它:JSON.parse(sessionStorage.getItem('POS'))
它不起作用,尽管我将查找sessionStorage.setItem('position',JSON.stringify(position.coords))和Listing.html
,获取方法:JSON.parse(sessionStorage.getItem('POS'))
感谢您的建议,但它也不起作用。实际上,它没有显示任何错误,但没有显示所需的输出。尽管在你的建议下,我使用了localstorage,它仍然有效。var Latitude=localstorage.getItem('latitudeGet');var Longitude=localStorage.getItem('longitudeGet')
Thanksh抱歉,我没有听清楚listing.htmlThanksh中的部分内容。这些只是一些其他问题
window.location="Listing.html?lat="+p.coords.latitude+"&lgt="+p.coords.longitude;