Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
JS redirect based on browser language

I'm using this test code:

<script type="type/javascript">

$( document ).ready(function(){
    var userLang = navigator.language || navigator.userLanguage;
    if (userLang == "fr") {
        window.location.href = "www.facebook.com"
    }
    else if(userLang == "de"){
       window.location.href = "www.facebook.com"
    }
    else {
        window.location.href = "www.google.com"
    }
});
</script>

But nothing happens. I must be dumb?

You can see it here: URL removed

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You were missing a protocol. Also, here's a suggestion for a cleaner approach replacing the series of if's.

var userLang = navigator.language || navigator.userLanguage;
var urls = {
    'fr': 'http://www.facebook.com',
    'de': 'http://www.facebook.com',
};
var defaultUrl = 'http://www.google.com';

var url = urls[userLang] ?? defaultUrl;
console.log(url);

//window.location.href = url;

about 4 years ago · Juan Pablo Isaza Report

0

// Better code in terms of lines of code and avoiding if conditions.

const sites = {
'fr': 'https://www.facebook.com',
'de': 'https://www.google.com',
};

const userLang = navigator.language || navigator.userLanguage;
window.location.href = sites[userLang];
about 4 years ago · Juan Pablo Isaza Report

0

Please give it a try like this:

<script>
  $( document ).ready(function(){
    var userLang = navigator.language || navigator.userLanguage;
    if (userLang == "fr") {
        window.location.href = "https://www.facebook.com"
    }
    else if(userLang == "de"){
       window.location.href = "https://www.facebook.com"
    }
    else {
        window.location.href = "https://www.google.com"
    }
});
</script>

And find this documentation MDN HTML Script tag regarding how to use the tag.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!