diff --git a/README.md b/README.md index c3401a5..a997d74 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ > Yokto simply means smallest SI prefix. -No framework framework 😛 +Simple/tiny jQuery replacement/alternative library 😛 -YoktoJS is supposed to be used with VanillaJS, using any frameworks* parallelly might bug things up! --- -\*frameworks as in: jQuery, etc. +YoktoJS is supposed to be used with VanillaJS, using any library/frameworks* parallelly might bug things up! +--- +\*library/frameworks as in: jQuery, etc. (Basically any library/framework that uses `$_`, `$`, `$$`, `_`, `__` since Yokto uses it.) diff --git a/yokto.js b/yokto.js index 5025d9b..15c20f2 100644 --- a/yokto.js +++ b/yokto.js @@ -23,24 +23,38 @@ const _ = (parentSelector, tag, attrs, innerText) => { }; parentElem.appendChild(elem); }; -const $$ = (callback) => { - window.addEventListener("DOMContentLoaded", () => { - callback(); +const $$ = (fn) => { + if (typeof fn !== 'function') { + throw new Error('Argument passed to ready should be a function'); + } + + if (document.readyState != 'loading') { + fn(); + } else if (document.addEventListener) { + document.addEventListener('DOMContentLoaded', fn, { + once: true // A boolean value indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked. }); + } else { + document.attachEvent('onreadystatechange', function() { + if (document.readyState != 'loading') + fn(); + }); + } }; const $_ = async (method, url, data) => { var headers = {}; if (data) { headers['Content-Type'] = 'application/json'; + headers['Accept'] = 'application/json'; }; const resp = await fetch(url, { method: method, - mode: 'same-origin', + mode: 'cors', cache: 'no-cache', credentials: 'same-origin', headers: headers, redirect: 'follow', - referrerPolicy: 'no-referrer', + referrerPolicy: 'unsafe-url', body: JSON.stringify(data) }); return await resp.json();