title: In JavaScript, can I get hold of the node that script is enclosed in?
body: <p>Is there any way in JavaScript to get hold of the parent node of the currently executing script node without have an id attribute on the script tag?</p>
  
  <p>To illustrate what I mean, if I want to append an img to the document, and I want to append that image to the div node with id "div1_id", can I do that without knowing the id of the div, or having to add the id="_scriptid" attribute to the script tag, as I have had to do below?</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function fn1()
      {
          var my_img = document.createElement("img");
   var t = document.getElementById("_scriptid");
   if (t.parentNode) {
    t.parentNode.appendChild(my_img);
   } else {
    document.getElementsByTagName("body")[0].appendChild(my_img);
   }
      }
  &lt;/script&gt;
  
  &lt;div id="_div1_id" name="_div1_name"&gt;
      &lt;script type="text/javascript" id="_scriptid"&gt;
          fn1();
      &lt;/script&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here's what I want to do:</p>
  
  <pre><code>&lt;head&gt;
    &lt;script type="text/javascript"&gt;
      function fn1()
      {
          var my_img = document.createElement("img");
   var x = get the node that is the parent node of the current script tag,
                  and so that I can still separate this code out into a function
                  as shown here, I do not want the &lt;head&gt; tag returned, I want to
                  get the parent node of the script that called this function, i.e.
                  the node commented as "div1" below.
   x.appendChild(my_img);
      }
    &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;div&gt;  &lt;!-- div1 --&gt;
      &lt;script type="text/javascript"&gt;
          // Call a function to add an image to the enclosing node (div node in this example):
          fn1();
      &lt;/script&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The reason I ask is that I am getting someone telling me they are getting an error in IE8 "HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)" and I suspect it may be because I am using appendChild to append an image to the body element and the body element is not closed. The KB article suggests that adding to the direct parent (even though that tag is obviously not closed) resolves the problem.</p>
  
  <p>Thanks.</p>