AgnosticRun is a lightweight utility for ensuring a DOM element exists before executing a function in vanilla JavaScript. It's useful when dealing with dynamic DOM manipulation or when you want to ensure that certain elements are present before applying any JavaScript logic to them.
To install AgnosticRun using npm, run the following command:
npm install agnosticrun
To use AgnosticRun, import the agnosticRun
function and wrap your own function that interacts with the DOM.
import { agnosticRun } from 'agnosticrun';
// Define your function that will interact with a DOM element
function updateElement(element, text) {
element.textContent = text;
}
// Wrap your function using agnosticRun to ensure the element exists
const safeUpdate = agnosticRun(updateElement);
// Call the wrapped function with the element's ID and the desired content
safeUpdate('element-id', 'Hello, World!');
In this example, AgnosticRun ensures that the element with ID element-id
exists before attempting to update its text content. If the element does not exist, the function will not execute.
AgnosticRun allows you to control the appearance of console.log
and console.warn
messages through optional flags. You can enable or disable these messages as needed using the following options:
debugLog
: Controls the appearance of informational messages (console.log
). It can be set totrue
orfalse
.debugWarn
: Controls the appearance of warning messages (console.warn
). It can be set totrue
orfalse
.
By default, both flags are disabled.
Show only warns on console:
window.agnosticRun = agnosticRun({ debugLog: false, debugWarn: true });
Show only debug messages on console:
window.agnosticRun = agnosticRun({ debugLog: true, debugWarn: false });
Doesn't show any message:
window.agnosticRun = agnosticRun;
-or-
window.agnosticRun = agnosticRun({ debugLog: false, debugWarn: false });
AgnosticRun is licensed under the MIT License. See the LICENSE file for details.
If you encounter any issues, feel free to report them here.