I've created an external/public JavaScript library.
As I am testing it on an Angular 1.x web app by including the script in the <HEAD>, I've found that it breaks the Angular app by making clicks do nothing.
After hours of debugging namespace, properties, and functions; I've finally found the culprit is when I insert HTML elements into the webpage. My JS library is currently using this code to insert HTML into the webpage. How would I go about making it support Angular apps (or SPAs in general)?
var HTML_ELEMENTS = ['<h1>Hi</h1>', '<div>Hello again.</div>'];
for (var i = 0; i < HTML_ELEMENTS.length; i++) {
document.body.innerHTML += HTML_ELEMENTS[i];
};
One of the reasons I add the HTML elements to the bottom of document.body is because I set these elements to have the highest z-index in the page, so any competing elements will yield the z-index to my inserted HTML elements.
innerHTML +=will replace the whole element content with the newly rendered HTML, containing of the current innerHTML + the new string you appended. In the process, all existing descendant elements will be destroyed, and then re-created, plus the new ones. Therefor you loose all previously attached event handlers, etc. If you want to do this in vanilla JS, you'll have to go the DOM way and use createElement, createTextNode, appendChild ...<div id="plugincontainer"></div>in your document, and then usedocument.getElementById("plugincontainer").innerHTML +=