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?
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
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.
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.