• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3890
  • Last Modified:

Javascript to identify browser language and redirect user to a specific page

Hello,

What is the exact Javascript to use in an HTML file to:
1. identify the browser language (needs to work with IE, Chrome, Mozilla, Safari, and the App versions thereof)
2. if the language is Dutch, redirect the user to another page (say start.html)

Please note I'm a  novice in this area and looking for code to copy paste, generic examples in internet that I still need to be adjust will not help much. Please note this needs to work on all four browsers incl. their mobile versions, even if it requires one set of code for each browser.

Thank you
0
TechNovation
Asked:
TechNovation
  • 4
  • 3
  • 3
1 Solution
 
Alexandre SimõesManager / Technology SpecialistCommented:
Please note I'm a  novice in this area and looking for code to copy paste
Being a novice isn't an excuse for copy/paste anything. For your own sake make sure you understand what the code does before using it anywhere.

This said, you can use javascript to do it. Something like:
var userLang = navigator.language || navigator.userLanguage;
switch(userLang){
    case 'en-US':
        window.location.href = '/en/home.html';
        break;
    case 'fr-FR':
        window.location.href = '/fr/home.html';
        break;
    default:
        // if it's something else fall back to the default
        window.location.href = '/en/home.html';
        break;
}

Open in new window

0
 
Alexandre SimõesManager / Technology SpecialistCommented:
I forgot about the explicit German request.
So you only want to redirect if it's German?

Here it is (just put this inside your head block):
<script type="javascript">
var userLang = navigator.language || navigator.userLanguage;
if(userLang.substring(0,2) == 'de'){
    window.location.href = '/start.html';
}
</script>

Open in new window

0
 
COBOLdinosaurCommented:
Of course you also need to understand that just because a browser identifies it self as a given product does not mean that is what it is, as the header can be modified from the client.  

The proper way to detect the browser and level is with object detection, and doing it to decide what page they need suggest that you intend to send a different page, or different components based on the browser.  Good luck with that because the supported features change with every new release.

So if you are a novice I suggest you spend some time on a few tutorials to get your skills up to the point where you understand the basics enough to avoid  walking into quicksand trying to do things the wrong way.

Generally Experts are not going to just write the code for you.  We collaborate on finding a solution.  Doing the work for you does nothing to help you or us.

Cd&
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
TechNovationAuthor Commented:
Good advice much appreciated, what I actually really want is to redirect all visitors from a country (Holland) to a specific page. But I need as much certainty as I possibly can get.
Can I use the country where the user is browsing from. If possible I would like double layer code where I redirect based on web browser language, and if this fails I catch it in the country (e.g. IP address).

Somehow some big companies' websites manage to tell I'm browsing from the Netherlands even though my browser language is always set to english. I need the code to be that strong.
Thank you
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Well that's another story...  completely...

First off, you need a third party API to give you that information. The browsers know nothing about their geographic location. Good ones are payed and from the free I know http://www.ipinfo.io

Then you have to keep in mind that this is highly inaccurate. Users behind a proxy or with some special ISP might get you a completely wrong information.

You say you're a noob but your getting into some more complicated waters where you need to do an AJAX call on page load to redirect if the country matches.
And what if the user lands on a page other that the home page?
Are you really willing to rely on an extra AJAX call every time a page loads?

Make sure this functionality is really important and that you can't just give the user the ability to manually change to its own country version of the site.
Take Amazon as an example for instance.

Cheers!
0
 
COBOLdinosaurCommented:
That is good advice from AlexCode.  The only way you can ever be sure that the user has the version of the page they prefer is to ask them what they want.  That is the only method that is 100% effective.  I don't think anything else would come up with more than 60-70%, and just starting out it is unlikely you can implement without hiring a professional develper to write, test and implement the code necessary.

You can avoid having to question the user more than once by using a cookie to save the selected choice and use that to retrieve the value into a session variable so you do not have to re-direct for every page.  Plus you can avoid ugly urls by organizing the file system so the pages for a given language/location is in a directory of its own.

Cd&
0
 
TechNovationAuthor Commented:
Thank you. The purpose is actually not to make website content better readable for Holland, but to DENY access to a specific website to all viewers based in Holland.

Result should be:
- Users outside Holland get full access
- Users within Holland are redirected to a page that only says "website in construction", they should not know they are filtered out, but only get the impression content is yet to come.

I'm looking for any combination of code that will use any parameters to filter Holland out (language, IP address, whatever...). This is why asking the user will not help.

We want to delay access of the country by some 2 years using whatever code possible.

Thank you
0
 
COBOLdinosaurCommented:
There is no method that is going to give you more than 60-70%.

Your best be t is to use something like: http://dev.maxmind.com/geoip/legacy/geolite/
to detect geolocation based on IP and you may be able to refine it and improve on it over time.

if you do a search on geolocation database  you may find better database solutions or an API that works for you. Perhaps a combination of methods; however in the end you will always be limited to some fraction of the total target traffic.  An additional downside is that you will almost certainly re-direct some traffic that you do not want to re-direct.

Cd&
0
 
TechNovationAuthor Commented:
You know what. I think I ended up asking 2 questions in one. Let me first close the language part of it and open a new one on the geographic aspect.
I have to admit none of the code above worked.
in the last code I see: "/start.html"
 it may be an issue with the /
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Also think about the credibility of your website.

People in Holland can eventually get out of the country and see your site as well as people that have access to it suddenly get a "In Construction" page if they access it within Holland?

This seems like a pretty unreliable design from the user perspective and a very fragile "security" for you.

I have no clue about your motivations but big sites like Spotify and Netflix do block content based on the user's geographic location but in a much transparent way.
The user can access the site but they get an information that it's not available in their country.
Of course you can "override" this "security" using any of the methods already described but at least your not "lying" to anyone.

Above all, keep in mind that if the users want to access the site they will.
If it's really important to block the content, you have to find another way.

Cheers!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now