Dual Language Browsers Redirect Javascript
I am trying to redirect English browsers to xyz.com/?lang=en while letting Swedish ones stay on xyz.com I have been trying : var type=navigator.appName if (type=='Netscape') var la
Solution 1:
if (lang !== "sv") {
window.location.replace(window.location.href + '?lang=en');
}
Solution 2:
You are always checking the language from the navigator
.
This way everytime the page loads it will try to reload the page with the altered or not url.
You need to place a condition where it will not reload the page.. That should be when there is a URL parameter passed.
So check for the querystring as well and override the default if a value exists in the url.
<scripttype="text/javascript">
(function(undefined){
var queryString = {},
search = window.location.search.substring(1), // read the current querystring
searchItems = search.length?search.split('&'):[]; // and split it at the different params if any existfor(var i = 0, len = searchItems.length; i < len; i++){ // for each parameter passedvar parts = searchItems[i].split('='); // split the key/value pair
queryString[parts[0].toLowerCase()] = parts[1]; // and store it to our queryString variable
}
if (queryString.lang === undefined){ // if there is no lang parameter passed do your checking otherwise skip this step completely and use the url lang parameter.var type=navigator.appName, lang = '';
if (type=="Netscape") {
lang = navigator.language.substr(0,2);
} else {
lang = navigator.userLanguage.substr(0,2);
}
if (lang != "sv"){
if (searchItems.length){
window.location.replace(window.location.href + '&lang=en');
} else {
window.location.replace(window.location.href + '?lang=en');
}
}
}
}());
</script>
Notice: Although, as @missingno mentions in his comment to your question, this is best handled server-side than client-side.
Post a Comment for "Dual Language Browsers Redirect Javascript"