英文:
Looping in window.location
问题
I am getting lat and lng from sessionStorage and I want to send it to browser with window.location but i get to an infinity loop.
从sessionStorage获取经度(lat)和纬度(lng),然后尝试使用window.location将它们发送到浏览器,但出现了无限循环的问题。
var lat = sessionStorage.getItem('lat');
var lng = sessionStorage.getItem('lng');
window.location = '?lat=' + lat + '&lng=' + lng;
I saw other questions about this but they did not work.
I try to use window.location.hash / href etc... but the result is not what I expect.
我看到其他相关的问题,但它们没有起作用。
我尝试使用window.location.hash / href等等,但结果不是我期望的。
Does someone know how to build that function to work properly?
Thank you very much.
有人知道如何正确构建这个函数吗?
非常感谢。
英文:
I am getting lat and lng from sessionStorage and I want to send it to browser with window.location but i get to a infinity loop.
var lat = sessionStorage.getItem('lat');
var lng = sessionStorage.getItem('lng');
window.location = '?lat='+lat+'&lng='+lng;
I saw other questions about this but they did not work.
I tryed to use window.location.hash / href etc... but the result is not what I expect
Does someone knows how to build that function to work properly?
Thank you very much
答案1
得分: 2
Sure, here are the translated code parts:
在设置位置之前,请检查以确保不会陷入循环。
const urlParams = new URLSearchParams(window.location.search);
const latQS = urlParams.get('lat');
const lat = sessionStorage.getItem('lat');
const lng = sessionStorage.getItem('lng');
if (!latQS && lat && lng) {
const lat = sessionStorage.getItem('lat');
const lng = sessionStorage.getItem('lng');
window.location = '?lat=' + lat + '&lng=' + lng;
}
另一种选择是使用pushstate设置历史记录。
const lat = sessionStorage.getItem('lat');
const lng = sessionStorage.getItem('lng');
if (lat && lng) {
const url = new URL(window.location);
url.searchParams.set('lat', lat);
url.searchParams.set('lng', lng);
window.history.pushState({}, '', url);
}
Please note that I have removed the HTML entity codes and translated the comments in the code.
英文:
Check before setting the location so you are not stuck in the loop.
const urlParams = new URLSearchParams(window.location.search);
const latQS = urlParams.get('lat');
const lat = sessionStorage.getItem('lat');
cost lng = sessionStorage.getItem('lng');
if (!latQS && lat && lng) {
const lat = sessionStorage.getItem('lat');
cost lng = sessionStorage.getItem('lng');
window.location = '?lat='+lat+'&lng='+lng;
}
Other option is setting the history with pushstate
const lat = sessionStorage.getItem('lat');
cost lng = sessionStorage.getItem('lng');
if (lat && lng) {
const url = new URL(window.location);
url.searchParams.set('lat', lat);
url.searchParams.set('lng', lng);
window.history.pushState({}, '', url);
}
答案2
得分: 1
以下是您要翻译的内容:
这是一个自调用函数,用于实现您想要的效果
const redirect = (() => {
var lat = sessionStorage.getItem("lat");
var lng = sessionStorage.getItem("lng");
const params = new URLSearchParams(window.location.search);
!params.get("lat") ? (window.location.href = `?lat=${lat}&lng=${lng}`) : null;
})();
英文:
Here is a self invoked function to achieve what you want
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const redirect = (() => {
var lat = sessionStorage.getItem("lat");
var lng = sessionStorage.getItem("lng");
const params = new URLSearchParams(window.location.search);
!params.get("lat") ? (window.location.href = `?lat=${lat}&lng=${lng}`) : null;
})();
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论