0

In my html code I have this inline script (no src)

<script>
    var div = document.createElement("div");
    // insert div after this script tag
</script>

How can I then insert the div into the DOM with javascript such that it appears right after the script tag, so it ends up looking like this

<script>
    var div = document.createElement("div");
    // insert div
</script>
<div></div>

The main thing is I want a solution that avoids using id's, class's and attributes to try to do a lookup. Is there a way I can do something like getting the current script node and then a insertAfter method?

Thanks

omega
  • 40,311
  • 81
  • 251
  • 474
  • Does this script appear at a particular position? Meaning does it always appear as the top most? Or is this tag inserted dynamically (asynchronously) via code as well? – Jaya Sep 06 '17 at 23:23
  • the script tag it self is not dynamic, but why should that matter anyways. It will be in the dom when it executes. – omega Sep 06 '17 at 23:26
  • It does matter because when you are trying to find this specific script before its added to the page the returned element will be undefined and it may still be loading and being added to the page. – Jaya Sep 06 '17 at 23:30

0 Answers0