2

I am building a multilingual site and I want to insert a function, on initial page load, that finds the default language setting of the browser and then prompts the user to choose which language they would like. (Eng/Esp)

Of course the language in the prompt would be in the same language of the browser. I already have navigation between these languages on all pages page but I want to intercept the user upon entrance and avoid using a splash page.

Can anybody advise me on how this is done?

Adam Brown
  • 2,812
  • 4
  • 28
  • 39

3 Answers3

3

First you need a custom event listener

function addEvent(to, type, fn) {
    // Firefox, Safari, Chrome, Opera
    if(document.addEventListener) {
        to.addEventListener(type, fn, false)
    }
    // Microsoft ActiveX Scripts
    else if(document.attachEvent) {  
        to.attachEvent('on'+type, fn)
    }
    // Last hope
    else {  
        to['on'+type] = fn
    }
}

Add event listener on the window when its loaded and run the function onDomLoaded()

addEvent(window, 'load', onDomLoaded)

Create the function onDomLoaded

function onDomLoaded() {
    alert('Im finished loading the entire window, your language is: ' + navigator.language)

}

Here the example on jsFiddle

Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
  • Thats amazing! And then I would add something within the final function to choose between. Make another function outside of it and then call it within? – Adam Brown Feb 28 '13 at 14:25
  • Can be done both. Use the `onDomLoaded` function as the `init`/`document class`/`constructor` of your javascript. Be creative :) – Ron van der Heijden Feb 28 '13 at 14:28
1

Like Deadlock has said you can use a JavaScript Modal pop-up. Someone has given an example of this Here.

You can create this pop-up containing buttons to select the language.

Community
  • 1
  • 1
Will
  • 3,004
  • 29
  • 43
  • Thank you that looks like it may be really useful. Would the method be to replace the *null* terms for the address of the page? – Adam Brown Feb 28 '13 at 14:10
  • Not too sure havent looked into too much. If you just google javascript or jquery modal pop. There are pre-built bits of code you can use that are simple to use. – Will Feb 28 '13 at 14:15
1

Answer on how to get the browser language using JavaScript. And as you specified PHP in your question tags, answer to do so with PHP.

Community
  • 1
  • 1
Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28
  • This is great I might use the second comment on the PHP page becuase it seems very easy to implement. Thank you – Adam Brown Feb 28 '13 at 14:24