I'm new to programming. Decided to create and launch my first website. It consists only of 2 html pages. Page one is in Russian (default) and page 2 is in English.
I want JS to redirect to the English page all the users who have their browsers languages not in Russian.
So I tried to use this code.
<script type="text/javascript">
$( document ).ready(function(){
var userLang = navigator.language || navigator.userLanguage;
if (userLang == "ru") {
break;
}
else {
window.location.href = "/en/index.html"
}
});
</script>
But nothing happened.
Your break
statement is invalid there. Also, what you're trying to achieve doesn't need an else statement - you can redirect user to the english version if
the language is not russian.
<script type="text/javascript">
$( document ).ready(function(){
var userLang = navigator.language || navigator.userLanguage;
if (userLang !== "ru") {
window.location.href = "/en/index.html";
}
});
</script>
This is your code after a little tweak. As someone already mentioned - you could achieve that easily without jQuery.
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.