Search code examples
phpjavascripthtmlpopupmultilingual

Choose language popup on page load


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?


Solution

  • 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