在 window.location 中循环。

huangapple go评论67阅读模式
英文:

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 = (() =&gt; {
  var lat = sessionStorage.getItem(&quot;lat&quot;);
  var lng = sessionStorage.getItem(&quot;lng&quot;);
  const params = new URLSearchParams(window.location.search);
  !params.get(&quot;lat&quot;) ? (window.location.href = `?lat=${lat}&amp;lng=${lng}`) : null;
})();

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 05:14:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定