[英]How to Clear then Append URL with JavaScript
I am trying to reload a page after a few seconds and append the URL with a variable containing new Lat Long coordinates. 我试图在几秒钟后重新加载页面,并在URL后面附加包含新的Lat Long坐标的变量。 Consider the code snippet below that works with Leaflet :
考虑下面适用于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);
}
Does anyone know how to clear and then append the URL? 有谁知道如何清除然后附加URL? Thanks.
谢谢。
Build the url using the parts 使用各部分构建URL
window.location.href = [location.protocol, '//', location.host, location.pathname, loadLatlng].join("");
Same thing as 与...相同
window.location.href = location.protocol + '//' + location.host + location.pathname + loadLatlng;
You could also do it a dirty way with splitting the current url on the ?
您还可以通过在
?
上拆分当前网址来做到这一点?
window.location.href = window.location.href.split("?")[0] + loadLatlng;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.