I have been trying to understand why my js code which was inside the HTML is not working now that it is in an external file. English is not my first language which makes me even harder to understand (and spanish results haven't been quite productive as I hoped).
I literally copy-paste the code to the other file and placed <script src="core.js"></script>
to get the js loaded but both Firefox and Chrome keep telling me "TypeError: eng is undefined"
when it is.
Before anyone tells me, it was already responded to in other questions, yes, I saw it was. But I don't understand anything so if anyone could explain me like "JS for dummies" would be REALLY appreciated.
The code is:
//SETS "LOCALE" "EN" TO REDIRECT TO ENGLISH
(function (){
'use strict';
var eng = document.querySelector('.eng')
function setLocalStorage(){
eng.addEventListener('click', () => {
localStorage.setItem('locale','en')
})
}
setLocalStorage()
}());
//SETS "LOCALE" "ES" TO REDIRECT TO SPANISH
(function (){
'use strict';
var esp = document.querySelector('.esp')
function setLocalStorage(){
esp.addEventListener('click', () => {
localStorage.setItem('locale','es')
})
}
setLocalStorage()
}());
//GETS THE "LOCALE" AND REDIRECT
(function () {
'use strict';
const locale = localStorage.getItem('locale');
if (locale === 'en') {
window.location = 'eng.html';
} else if (locale === 'es') {
window.location = 'esp.html';
} else {
// first visit:
document
.querySelector('.eng')
.addEventListener('click', () => {
localStorage.setItem('locale', 'en')
});
document
.querySelector('.esp')
.addEventListener('click', () => {
localStorage.setItem('locale', 'es')
});
}
}());