0

I am trying to change the page url based on where the user is located. Using the ipapi, I am pulling the city name, and trying to insert it in the url as a variable when the page loads. For some reason, my page keeps refreshing continuously. Any help would be great!

I believe I need a condition to stop the window.location.replace after running once. Or I need the city variable and window.location.replace to be outside of the function. But I am currently stumped.

fetch('https://ipapi.co/json/')
.then(function(response) {
  response.json().then(jsonData => {
    console.log(jsonData);
    var city = jsonData.city;
   window.location.replace("https://preapproval.mortgagestack.ca/lethbridge/? 
 location=" + city);
 });
})
.catch(function(error) {
console.log(error)
});
Kyle White
  • 89
  • 5

0 Answers0