As wonko79 already pointed out, the problem is that inside your if
checks you actually reassign the variable instead of checking its content. To actually check the string contents for equality, you need to use two equals signs ==
(or three if you want to check for type equality too).
Furthermore, whenever you keep repeating yourself like that, it’s a good sign that you can improve a lot about your code. In this case, you always check if localStorage.language
equals to a certain language, and if that’s the case, you then change the location to that language’s URL. So essentially, you are looking up the URL for the language stored in the local storage.
JavaScript has an efficient data structure for lookups: Objects. So, to improve your situation, we can first store the data in an object:
var languageUrls = {
'language': 'language.html',
'english': 'eng.html',
'french': 'french.html',
'spanish': 'spanish.html'
};
After we have done that, we can simply look up the values immediately based on the value of localStorage.language
:
window.onload = function () {
// we can perform the lookup by providing the *key* in brackets:
var url = languageUrls[localStorage.language];
// in case the key did not exist in our object, url will be null
if (!url) {
// so we might want to provide a fallback value here
url = 'eng.html';
}
// now we have a valid URL in our `url` variable
window.location.href = url;
}
And that’s all you need to do here; no need to repeatedly check with if statements, just a simple lookup. And when you want to add another language, you just need to add another entry to the languageUrls
object.
A similar idea can be applied to your language-functions which change the language stored in the local storage. Instead of having multiple different functions, it’s enough to have a single function which takes the language as a parameter and then again uses the lookup to change the URL:
function changeLanguage (language) {
// it’s always a good idea to normalize input;
// in this case, keep everything lower-case
language = language.toLowerCase();
// this time, we might want to make sure first that the language is
// valid – all valid languages are stored in our `languageUrls` object,
// so we can use that
var url = languageUrl[language];
if (!url) {
// There was no entry for the language—it’s not a valid language—so
// we just return from the function here, not doing anything. We could
// show an error though.
return;
}
// so the language is valid, and the target URL is stored in `url`
if (typeof(Storage) !== 'undefined') {
// update the stored language; note that I’m not calling `clear`
// because we might want to store other information in the future
// too. It’s enough to just overwrite the stored language.
localStorage.language = language;
}
// we still want to redirect, even if the storage was not available
window.location.href = url;
}
And that’s already everything you need to set up. All that’s left is changing the calls from english()
to changeLanguage('english')
etc.