I am trying to make a plugging using JS.
I have the following code :
var fileref = document.createElement('script');
fileref.src = "js/index.js";
document.documentElement.appendChild(fileref);
I can verify that the code loads across domain however it doesn’t always execute. Even if I set it before </head>
Sometimes it does but sometimes it just doesn’t. Any ideas on what I could be doing wrong?
The JavaScript is sometimes running before the
document.documentElementexists. So when it goes to append the newscriptelement, it fails. The inconsistency (“sometimes it does but sometimes it just doesn’t”) is due to the page rendering at ever-so-slightly different speeds.Update:
Put this in your
<head>: