Find DOM position of <script> section

I am writing a small library that should be used by people who know nothing about programming and it should add a "complex" html component with one or two lines of javascript (for example with a form with some inputs and texts insert email and other credentials ).

Is it possible to "self-determine" the position of a script to add an element to the DOM at that position? I mean, for example in code like this

<div>
    <p> ... </p>
    <script>
         // my code is here
    </script>
    <a> ... </a>
    ...
</div>

      

I want to add an element at the position where the script is written (son of div and after p), assuming that no element has a specific id. Is it possible?

+3


source to share


1 answer


Unfortunately, this document.write()

is the only way to do it.



You can abuse this to get the contained element by creating an element with a unique ID and then accessing .parentNode

that element.

+1


source







All Articles