【发布时间】:2022-08-19 02:32:02
【问题描述】:
我有一个要求地理位置的网站。它应该只在您第一次打开网站时询问,但我注意到如果您在移动设备(使用 Chrome 或 Safari)或使用 Safari 的桌面上打开网站,则每次重新加载页面时网站都会请求许可。
如果您在使用 Chrome 的计算机上打开该网站,它可以正常工作,没有任何问题。
这是我的代码。这个问题的原因是什么?
jQuery(document).ready(function($) {
if (\"geolocation\" in navigator) {
console.log(\"Geolocation availbale\");
navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError);
} else {
geolocationError()
}
function geolocationSuccess(position) {
console.log(position.coords.latitude, position.coords.longitude);
$.ajax({
url: geolocationParams.ajaxurl,
type: \'POST\',
data: {
action: geolocationParams.actionLocalities,
lat: position.coords.latitude,
lng: position.coords.longitude,
},
dataType: \'json\',
success: function(response) {
console.log(\"Ajax returned\", response);
$(\'.homepage-posts-wrapper.third-block-grid\').html(response.html);
}
})
}
function geolocationError(err) {
console.log(err);
$.ajax({
url: geolocationParams.ajaxurl,
type: \'POST\',
data: {
action: geolocationParams.actionLocalities,
lat: \"\",
lng: \"\",
},
dataType: \'json\',
success: function(response) {
console.log(\"Ajax returned\", response);
$(\'.homepage-posts-wrapper.third-block-grid\').html(response.html);
}
})
}
});
标签: javascript wordpress geolocation