Is it possible to insert DOM HTML element at the point of execution of <script>
tag? Like with document.write()
function which immediately inserts text into DOM when executed.
[UPDATE]
Yes it is! Here's what I came up with:
var injectElement = function (element) {
//Function that generates random id
var randomID = function () {
var id = '';
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for( var i=0; i < 5; i++ )
id += chars.charAt(Math.floor(Math.random() * chars.length));
return id;
};
var id = randomID();
//Adding placeholder element
document.write('<div id="' + id + '"></div>');
//Injecting new element instead of placeholder
var placeholder = document.getElementById(id)
placeholder.parentNode.replaceChild(element, placeholder);
};