I'm using this test code:
<script type="type/javascript">
$( document ).ready(function(){
var userLang = navigator.language || navigator.userLanguage;
if (userLang == "fr") {
window.location.href = "www.facebook.com"
}
else if(userLang == "de"){
window.location.href = "www.facebook.com"
}
else {
window.location.href = "www.google.com"
}
});
</script>
But nothing happens. I must be dumb?
You can see it here: URL removed
You were missing a protocol. Also, here's a suggestion for a cleaner approach replacing the series of if's.
var userLang = navigator.language || navigator.userLanguage; var urls = { 'fr': 'http://www.facebook.com', 'de': 'http://www.facebook.com', }; var defaultUrl = 'http://www.google.com'; var url = urls[userLang]?? defaultUrl; console.log(url); //window.location.href = url;
// Better code in terms of lines of code and avoiding if conditions.
const sites = {
'fr': 'https://www.facebook.com',
'de': 'https://www.google.com',
};
const userLang = navigator.language || navigator.userLanguage;
window.location.href = sites[userLang];
Please give it a try like this:
<script>
$( document ).ready(function(){
var userLang = navigator.language || navigator.userLanguage;
if (userLang == "fr") {
window.location.href = "https://www.facebook.com"
}
else if(userLang == "de"){
window.location.href = "https://www.facebook.com"
}
else {
window.location.href = "https://www.google.com"
}
});
</script>
And find this documentation MDN HTML Script tag regarding how to use the tag.
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.