MSDN: insertAdjacentHTML method[
^]: Remarks section in the document says:
You cannot insert text while the document is loading. Wait for the onload event to fire before attempting to call this method.
When using the insertAdjacentHTML method to insert script, you must include the DEFER attribute in the script element.
Quite possibly your document has not completely loaded. If you are using jQuery there is a very handy method
document.ready()[
^] which will be called only after your document has completely loaded. You can put your code inside this method. If you are not sure, make sure your document has completely loaded before this code is called.
Hope that helps!