Certain environments generate iframe
urls you can't control. In CRM Dynamics, for example, I had the same thought. I have common web resources, but when included as a dependency of another resource the library ended up downloading again. I wanted the library to cross the wire once but that didn't happen.
Even if you manage getting your library to load once as I eventually did you'll have issues sharing it across iframes
. The host objects and types (e.g. classes) are unique to the iframe
.
function isHTMLDocument(el){ //impure
return el instanceof HTMLDocument;
}
function dropdown(values){ //impure
const el = document.createElement("select");
...
return el;
}
If you call methods against foreign iframes
hopefully you can see the problem. HTMLDocument
in the context where your library loads will not be the same as HTMLDocument
in a foreign iframe
. While isHTMLDocument
will work on document
in the local environment it won't work in the foreign one.
Same with document
and many libraries bake document
into functions to avoid requiring you to pass it in again and again. Unfortunately, due to each environment being its own sandbox, host-environment tainted functions are effectively broken.
Only pure functions would work when used in a foreign context like an iframe
document.
function is(el, type){ //pure
return el instanceof type;
}
function dropdown(document, values){ //pure
const el = document.createElement("select");
...
return el;
}