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')
});
}
}());
Actually according to your code you are accessing the DOM
element and further you are calling them to do some task. But suppose you are trying to access some unavailable DOM
content and then telling them to do something. In that case if you are trying to access some DOM
but javascript couldn't find that it will return undefined
. That's why you'll get undefined
. (i'm trying to explain it easily)
Please make sure you have some elements with the class name eng
and esp
.
So, to overcome this case you need to check if you can access them and then give some task to do.
Suppose i'm checking here if eng
is present there and then assigning some task to it.
eng && eng.addEventListener('click', () => {
localStorage.setItem('locale','en')
})
Full Code is here.
//SETS "LOCALE" "EN" TO REDIRECT TO ENGLISH
(
function (){
'use strict';
var eng = document.querySelector('.eng')
function setLocalStorage(){
eng && 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 && 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')
});
}
}());