Javascript 我希望页面只加载一次
我希望我的页面只被加载一次,发生的是它不断地重新加载,请帮助我解决这个问题 我希望代码在页面加载时立即运行,但我不希望它被重新加载,它一直在重新加载我的页面 除了重新加载问题之外,一切都很正常Javascript 我希望页面只加载一次,javascript,jquery,ajax,html,Javascript,Jquery,Ajax,Html,我希望我的页面只被加载一次,发生的是它不断地重新加载,请帮助我解决这个问题 我希望代码在页面加载时立即运行,但我不希望它被重新加载,它一直在重新加载我的页面 除了重新加载问题之外,一切都很正常 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body onload="getLocation()">
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(redirectToPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function redirectToPosition(position) {
window.location='j3.php?lat='+position.coords.latitude+'&long='+position.coords.longitude;
}
</script>
<?php
$lat=(isset($_GET['lat']))?$_GET['lat']:'';
$long=(isset($_GET['long']))?$_GET['long']:'';
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $long;
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
$returned_contents_array = json_decode($returned_contents, true);
$results = $returned_contents_array['results'];
foreach($results as $result) {
echo $result['address_components'][0]['long_name'] . ", <br>";
}
?>
</body>
</html>
var x=document.getElementById(“演示”);
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(重定向拓扑位置);
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
功能位置(位置){
window.location='j3.php?lat='+position.coords.latitude+'&long='+position.coords.longitude;
}
检查函数内是否设置了坐标,如何:
function getLocation() {
<?php if (!isset($_GET['lat']) || !isset($_GET['long'])) { ?>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(redirectToPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
<?php } ?>
}
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(重定向拓扑位置);
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
这样,如果两个坐标参数都已设置,则函数体将为空且不会执行任何操作。每次将页面加载到支持地理位置的浏览器中时,执行设置新位置的
redirectToPosition
,重新加载页面并执行设置新位置的redirectToPosition
,重新加载页面并执行设置新位置的redirectToPosition
,重新加载页面并执行设置新位置的redirectToPosition
,重新加载页面并执行设置新位置的redirectToPosition
,重新加载页面并执行设置新位置的redirectToPosition
,重新加载页面,然后…哇,你真是个了不起的人,超级简单的解决方案,非常感谢billynoah;)