Cuando se cargó la página, después de agregar el parámetro de consulta a la URL, redirigí a esa URL.
Sin embargo, se produce un bucle infinito.
¿Cómo puedo implementar esto para que cuando se cargue la página, el valor se envíe desde JS a Rails y no provoque un bucle infinito?
window.onload = () => { function successGetPosition(position) { sessionStorage.setItem('latitude', position.coords.latitude); sessionStorage.setItem('longitude', position.coords.longitude); window.location.href = `/?latitude=${sessionStorage.getItem('latitude')}&longitude=${sessionStorage.getItem('longitude')}`; } function failGetPosition(error) { ... } options = {enableHighAccuracy: true}; function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(successGetPosition, failGetPosition, {enableHighAccuracy: true}); } else { alert("Geolocation is not supported by this browser."); } } getLocation(); };Dentro de esta función, lo primero que debe hacer es verificar si la URL contiene parámetros de consulta de latitud y longitud.
function successGetPosition(position) { // parse the url looking for latitude and longitude params // if they are there, return immediately else proceed with the code below const [latitude, longitude] = myUrlParseFn(); if (latitude && longitude) return; sessionStorage.setItem('latitude', position.coords.latitude); sessionStorage.setItem('longitude', position.coords.longitude); window.location.href = `/?latitude=${sessionStorage.getItem('latitude')}&longitude=${sessionStorage.getItem('longitude')}`; }