Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find DOM position of a <script> section

I'm writing a little library that should be used by people who don't know anything about programming, and it should add "complex" html component with one or two javascript lines (like a form with some inputs and texts to insert email and other credentials).

It's possible to "self-identify" script position to add element to DOM in that position? I mean, for example, in a code like this

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

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

like image 592
Naigel Avatar asked Nov 22 '25 12:11

Naigel


1 Answers

Unfortunately document.write() is the only way to do this.

You could abuse this to get the containing element though by creating an element with a unique ID and then accessing the .parentNode of this element.

like image 106
ThiefMaster Avatar answered Nov 24 '25 00:11

ThiefMaster