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.
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.
jquery
code here. Maybe you meant javascript
.
– abhishekkannojia
Commented
Jul 20, 2017 at 12:39
document.body.innerHTML = elem
. You need to remove the +
sign.
– Milan Chheda
Commented
Jul 20, 2017 at 12:40
Using x.innerHTML += y
is equivalent to x.innerHTML = x.innerHTML + y;
This means that you are pletely overwriting the old document with a new document - it may appear visually the same, but under the hood you've just nuked every single reference to everything.
If a bit of JavaScript elsewhere in the page used something like var container = document.getElementById('container');
, in order to save a reference, well that reference is now gone.
If there are any event listeners bound to elements in the document, those are gone too because the elements were nuked and replaced with identical-looking ones.
If you want to add your context menu to the page, you should do something like:
var menu = document.createElement('div');
menu.id = 'menu';
menu.innerHTML = "Your menu HTML here";
document.body.appendChild(menu);
This will add the new element to the page without nuking the whole thing.
document.body.innerHTML += "content";
Does three things:
innerHTML
innerHTML
with the new valueThis deletes the page and then creates a new one.
Since script elements inserted with innerHTML
are not executed, this kills the JS.
Don't append data using innerHTML
. Generate DOM nodes (with createElement
, createTextNode
and friends) and then append them (with appendChild
, insertBefore
and so on).
As has been explained by others, using:
document.body.innerHTML += newHtmlText;
deletes the entire page and recreates it. This causes:
.parentNode
). However, the JavaScript will be pletely ineffective at manipulating the displayed DOM, as it will be using references to elements which are no longer in the DOM.This will almost certainly pletely break most already existing JavaScript.
.insertAdjacentHTML()
The correct way to add HTML text, without disturbing the already existing contents, is to use element.insertAdjacentHTML(relativeLocation, HTMLtext)
. Using .insertAdjacentHTML()
, you can add HTML text in four different locations relative to the referenced element. The value of relativeLocation
can be:
'beforebegin'
: Prior to the element'afterbegin'
: Just inside the beginning of element (like adding a new .firstChild
, but you can add as many children (as much HTML text) as you desire).'beforeend'
: Prior to the end of the element (like .appendChild()
, but you can add as many children (as much HTML text) as you desire).'afterend'
: Just after the element (like element.parentNode.insertBefore(newElement,element.nextSibling)
, but you can add as many children of the parentNode (as much HTML text) as you desire). Note: If you were inserting an element you could also use: element.insertAdjacentElement('afterend',newElement)
.For what you desire to do, you would use:
document.body.insertAdjacentHTML('beforeend',newHtmlText);