Trying to change a string into a function without relying on eval() is proving to be troublesome. Whenever I pass the function name to the window object and check its type in JavaScript, it does not register as a function. Instead, I'm constantly met with a custom error message I've set up within the else statement: "Function not found: 1->validateLogin".
My custom dom_ready prototype:
dom_ready: function (inputFunc) {
document.addEventListener("DOMContentLoaded", function () {
try {
inputFunc();
} catch (e) {
DN.errors.push(e.message);
}
});
},
function show_pass() {
...
}
function validateLogin(k) {
...
}
DN.DOM.dom_ready(function () {
var event_ids = [
["#login-form-dialog", "validateLogin", "submit", 1],
["#loginnotes", "validateLogin", "click", 1],
["#loginnotes2", "validateLogin", "click", 2],
["#show-pass", "show_pass", "click", ""],
]
for (var i = 0; i < event_ids.length - 1; i++) {
var fN = window[event_ids[i][1]];
if (typeof fN === 'function') {
$(event_ids[i][0]).on(event_ids[i][2], function () {
fN(event_ids[i][3]);
})
} else {
console.log("Function not found: " + i + "->" + event_ids[i][1]);
}
}
});