无法重定向其他页面(HTML、Javascript)中的输出

Unable to redirect the output in another page(HTML,Javascript)

本文关键字:Javascript 输出 HTML 重定向 其他      更新时间:2023-09-26

我想找到当前位置的纬度和经度,并点击按钮在另一个页面上打印。虽然我成功地获得了纬度和经度,但无法在另一个页面上打印/重定向。

有人能帮忙吗。请找到下面的代码:

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

我想将输出重定向到列表页面。感谢

  • 使用CCD_ 1重定向到另一个页面&
  • event传递给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>

如果您的页面处理GET参数:

window.location="Listing.html?lat="+p.coords.latitude+"&lgt="+p.coords.longitude; 

否则,使用sessionStorage进行有状态应用程序:

sessionStorage.setItem('POS',JSON.stringify(position.coords))和在Listing.html中,通过:JSON.parse(sessionStorage.getItem('POS'))

获得

首先,脚本中不应该有p元素。它应该在身体里。此外,您的脚本标记应该位于正文的末尾,因为它包含修改DOM的代码。showPosition(position(函数应该位于getLocation((函数之前,因为您在getLocation中使用了showPosition。