Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

76
Vistas
I want to send values from JS to Rails when the page is loaded

Issue

When the page loaded, after adding the query parameter to the URL, I redirected to that URL.

However, an infinite loop occurs.

How can I implement this so that when the page is loaded, the value is sent from the JS to Rails and does not cause an infinite loop?

Code

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();
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Inside this function, the first thing you should do is check if the url contains latitude, longitude query params in it.

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')}`;
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda