• JavaScript – Redirecting Foreign Surfers

    Date: 2011.02.24 | Category: Scripts, WebDesign | Tags:

    At some point or another we are no doubt going to have the need to redirect some or all of our surfers based on the language they speak, this snippet of JavaScript when placed on your page will enable you to do just that without the need for .php or other more complex scripting.

    Here is the coding that you need to place between your <head> and </head> tags:

    <SCRIPT LANGUAGE=”JavaScript1.2″>
    <!– Begin
    if (navigator.appName == ‘Netscape’)
    var language = navigator.language;
    else
    var language = navigator.browserLanguage;

    if (language.indexOf(‘en’) > -1) document.location.href = ‘english.shtml';
    else if (language.indexOf(‘nl’) > -1) document.location.href = ‘dutch.shtml';
    else if (language.indexOf(‘fr’) > -1) document.location.href = ‘french.shtml';
    else if (language.indexOf(‘de’) > -1) document.location.href = ‘german.shtml';
    else if (language.indexOf(‘ja’) > -1) document.location.href = ‘japanese.shtml';
    else if (language.indexOf(‘it’) > -1) document.location.href = ‘italian.shtml';
    else if (language.indexOf(‘pt’) > -1) document.location.href = ‘portuguese.shtml';
    else if (language.indexOf(‘es’) > -1) document.location.href = ‘Spanish.shtml';
    else if (language.indexOf(‘sv’) > -1) document.location.href = ‘swedish.shtml';
    else if (language.indexOf(‘zh’) > -1) document.location.href = ‘chinese.shtml';
    else
    document.location.href = ‘english.shtml';
    // End –>
    </script>

    To add additional language redirects to this JavaScript all you need to do is duplicate the:

    else if (language.indexOf(‘zh’) > -1) document.location.href = ‘chinese.shtml';

    Section of the coding changing the (‘zh’) language code to that of the language you wish to redirect.

    Article written by Lee