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?
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?
Share Improve this question asked Feb 28, 2013 at 13:49 Adam BrownAdam Brown 2,8704 gold badges31 silver badges39 bronze badges 6-
Insert call to popup function in
$(document).ready(function(){ });
– Deadlock Commented Feb 28, 2013 at 13:56 - @Deadlock Hello, thank you, but my knowledge of JS is still very limited. I only understand basic console.log, return, print, etc. Could you explain a little more or perhaps you know a link? – Adam Brown Commented Feb 28, 2013 at 14:00
- @Deadlock I don't see a jQuery tag. – Ron van der Heijden Commented Feb 28, 2013 at 14:08
-
$
is a shortcut forjquery
@Bondye – Deadlock Commented Mar 1, 2013 at 4:46 - @Deadlock Lol I know that. But your ment you say he has to use jQuery for this, but the OP don't have the jquery tag bounded on the question... – Ron van der Heijden Commented Mar 1, 2013 at 7:35
3 Answers
Reset to default 3First 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.