Does anyone know why if you use document.body.innerHTML += "content";
the JavaScript on the page stops working? I have the following code:
document.addEventListener('contextmenu', function (e) {
e.preventDefault();
try {
document.getElementById('menu').remove();
} catch (x) {
//
}
var newHtmlText = "<div id='menu'>";
newHtmlText += "<div class='menu-item' id='menu-back' onclick='window.history.back();'>" +
"<div class='fa fa-arrow-left icon'></div><div class='text'>Back</div><div class='clear'></div></div>";
newHtmlText += "<div class='menu-item' id='menu-forward' onclick='window.history.forward();'>" +
"<div class='fa fa-arrow-right icon'></div><div class='text'>Forward</div><div class='clear'></div></div>";
newHtmlText += "<div class='menu-item' id='menu-reload' onclick='location.reload();'>" +
"<div class='fa fa-repeat icon'></div><div class='text'>Reload</div><div class='clear'></div></div>";
newHtmlText += "<hr />";
newHtmlText += "<div class='menu-item' id='menu-home' onclick='location.href = \"/\";'>" +
"<div class='fa fa-home icon'></div><div class='text'>Home</div><div class='clear'></div></div>";
newHtmlText += "</div>";
document.body.innerHTML += newHtmlText;
var menu = document.getElementById('menu');
menu.style.left = (e.clientX) + "px";
menu.style.top = (e.clientY) + "px";
});
Every time I open the context menu the JavaScript stopped working. This is not the only time it has done this.