Solved

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

Posted on 2014-03-11
12
1,680 Views
Last Modified: 2014-08-04
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
Comment
Question by:TechNovation
  • 4
  • 3
  • 3
12 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39920523
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
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39920542
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39921418
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
 

Author Comment

by:TechNovation
ID: 39931813
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
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39931954
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39935078
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
 

Author Comment

by:TechNovation
ID: 39935288
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39937323
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
 

Author Comment

by:TechNovation
ID: 39951113
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
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39951988
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now