Skip to content

Commit 97ac9c8

Browse files
committed
Handle multiple scripts loading a bit better
1 parent a4d40b8 commit 97ac9c8

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

simpletoast.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SimpleToast - A small library for toasts
33
*/
44
(() => {
5-
const version = buildVersion(1, 2);
5+
const version = buildVersion(1, 3);
66
if (window.SimpleToast) {
77
if (SimpleToast.version) {
88
if (SimpleToast.version >= version.number) return;
@@ -68,7 +68,7 @@
6868
}
6969

7070
const toasts = new Map();
71-
const root = (() => {
71+
let root = (() => {
7272
function create() {
7373
const el = document.createElement('div');
7474
el.setAttribute('id', 'AlertToast');
@@ -79,7 +79,17 @@
7979
body.appendChild(el);
8080
} else {
8181
window.addEventListener('load', () => {
82-
if (document.getElementById(el.id)) return; // Another script may have created it already
82+
const exists = document.getElementById(el.id);
83+
if (exists) { // Another script may have created it already
84+
if (el.hasChildNodes()) { // Transfer existing nodes to new root
85+
const nodes = el.childNodes;
86+
for (let i = 0, l = nodes.length; i < l; i++) {
87+
exists.appendChild(nodes[i]);
88+
}
89+
}
90+
root = exists; // Set this incase anyone still has a reference to this toast
91+
return;
92+
}
8393
document.getElementsByTagName('body')[0].appendChild(el);
8494
});
8595
}

0 commit comments

Comments
 (0)