我正在尝试在几秒钟后重新加载页面,并在URL后附加一个包含新Lat Long坐标的变量。考虑下面适用于 Leaflet 的代码片段:
var latlng = getQueryVariable("latlng");
if(latlng){
console.log(latlng); //working
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("?");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
function onMapClick(e) {//Coordinate pop up
popup.setLatLng(e.latlng)
.setContent("Coordinates clicked are: " + e.latlng.toString())
.openOn(map);
var centerPoint = map.getSize().divideBy(2),
targetPoint = centerPoint.subtract([1500, 0]),
targetLatLng = map.containerPointToLatLng(centerPoint), //retrieve new lat longs
loadLatlng = "?latlng="+targetLatLng.lat+","+targetLatLng.lng;
setTimeout(function(){
//And herein lies the problem - the URL is being concatenated ??
window.location.href = window.location.href + loadLatlng;
window.location.href.reload(1);
}, 5000);
}
有谁知道如何清除然后附加 URL?谢谢。
使用部件构建网址
window.location.href = [location.protocol, '//', location.host, location.pathname, loadLatlng].join("");
与
window.location.href = location.protocol + '//' + location.host + location.pathname + loadLatlng;
您也可以通过拆分?
上的当前 url 来以一种肮脏的方式做到这一点
window.location.href = window.location.href.split("?")[0] + loadLatlng;