gracefully handle when window[fnName] doesn't exist

This commit is contained in:
Isaac Connor 2019-07-23 10:03:28 -04:00
parent fe71a9abaa
commit df0aef89af
1 changed files with 21 additions and 1 deletions

View File

@ -144,7 +144,7 @@ window.addEventListener("DOMContentLoaded", function onSkinDCL() {
el.addEventListener("click", function onClick(evt) {
var el = this;
var url;
if (el.hasAttribute("href")) {
if ( el.hasAttribute("href") ) {
// <a>
url = el.getAttribute("href");
} else {
@ -167,12 +167,20 @@ window.addEventListener("DOMContentLoaded", function onSkinDCL() {
// 'data-on-click-this' calls the global function in the attribute value with the element when a click happens.
document.querySelectorAll("a[data-on-click-this], button[data-on-click-this], input[data-on-click-this]").forEach(function attachOnClick(el) {
var fnName = el.getAttribute("data-on-click-this");
if ( !window[fnName] ) {
console.error("Nothing found to bind to " + fnName);
return;
}
el.onclick = window[fnName].bind(el, el);
});
// 'data-on-click' calls the global function in the attribute value with no arguments when a click happens.
document.querySelectorAll("a[data-on-click], button[data-on-click], input[data-on-click]").forEach(function attachOnClick(el) {
var fnName = el.getAttribute("data-on-click");
if ( !window[fnName] ) {
console.error("Nothing found to bind to " + fnName);
return;
}
el.onclick = function() {
window[fnName]();
};
@ -181,6 +189,10 @@ window.addEventListener("DOMContentLoaded", function onSkinDCL() {
// 'data-on-click-true' calls the global function in the attribute value with no arguments when a click happens.
document.querySelectorAll("a[data-on-click-true], button[data-on-click-true], input[data-on-click-true]").forEach(function attachOnClick(el) {
var fnName = el.getAttribute("data-on-click-true");
if ( !window[fnName] ) {
console.error("Nothing found to bind to " + fnName);
return;
}
el.onclick = function() {
window[fnName](true);
};
@ -189,12 +201,20 @@ window.addEventListener("DOMContentLoaded", function onSkinDCL() {
// 'data-on-change-this' calls the global function in the attribute value with the element when a change happens.
document.querySelectorAll("select[data-on-change-this], input[data-on-change-this]").forEach(function attachOnChangeThis(el) {
var fnName = el.getAttribute("data-on-change-this");
if ( !window[fnName] ) {
console.error("Nothing found to bind to " + fnName);
return;
}
el.onchange = window[fnName].bind(el, el);
});
// 'data-on-change' adds an event listener for the global function in the attribute value when a change happens.
document.querySelectorAll("select[data-on-change], input[data-on-change]").forEach(function attachOnChange(el) {
var fnName = el.getAttribute("data-on-change");
if ( !window[fnName] ) {
console.error("Nothing found to bind to " + fnName);
return;
}
el.onchange = window[fnName];
});
});