Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS redirect from [example.com/home] to [example.com/home?loc=USA] causes infinite loop

I’m trying to redirect from example.com/home to example.com/home?loc=USA.
But it causes infinite loop. How do I stop it? Thanks.

My code:

<script>
    window.location = "example.com/home?loc=USA";
</script>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Currently you’re redirecting the user unconditionally, every time the page loads. You can wrap that logic in a condition which checks if the query string parameter you’re looking for exists or not. For example:

const params = new URLSearchParams(window.location.search);
const loc = params.get('loc');
if (!loc) {
  window.location = "example.com/home?loc=USA";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading