title: Running HTML/Javascript Dynamically
body: <p>I have a control on a web page that runs a call back function when a deposit is made. </p>
  
  <p>In the backend the user can add script, images, iframes, etc and my program creates a javascript function with the same name as the controls deposit call back that runs the snippets of code the user entered into the backend (script, iframe, html, etc).</p>
  
  <p>The function should run all the pieces of HTML/Javascript that a client entered into the CMS. I don't know in advance what the user will enter, it could be a script or an image or something else.</p>
  
  <p>I'm having trouble running the snippets of HTML/Javascript.</p>
  
  <p>1) I can't create a script element and add it to the DOM because it could be an image or something else entirely.</p>
  
  <p>2) document.write works but it erases the page.</p>
  
  <p>3) I can't use innerHTML, everything worked besides the scripts that I added dynamically.</p>
  
  <p>4) I can't use a textNode because it doesn't run the scripts when I add them to the DOM.</p>
  
  <p>Everytime I add it dynamically, I get the actual code printed to the string instead ofthe code running.</p>
  
  <p>Can anyone help?</p>
  
  <p>Thanks.</p>
  
  <p>p.s.</p>
  
  <p>Here is an example of some HTML, if I could get this to work where the trackingCode might be an image or a script then that's my answer:</p>
  
  <pre><code>&lt;head&gt;
  
  &lt;script type='text/javascript'&gt;
  
  function test(pixelData){
  
  var body= document.getElementsByTagName('body')[0];
  
  var trackingCode = '&amp;lt;script type=\'text/javascript\'&amp;gt;alert(\'[hi]\');&amp;lt;/script&amp;gt;';
  
  var div = document.createElement('div')
  
  trackingCode = trackingCode.replace(/&amp;gt;/g,'&gt;').replace(/&amp;lt;/g,'&lt;');
  div.innerText = trackingCode;
  
  document.documentElement.firstChild.appendChild(div);
  
  body.appendChild(div);
  
  
  }
  
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;script type="text/javascript"&gt;
  test();
  &lt;/script&gt;
  &lt;div id='whatever'&gt;
  
  &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>