Have JS in iFrame get value from element in main page
<p>This should be an easy one, but im having troubles.  I have an iframe that loads an .aspx page.  This .aspx page (one in the iframe) has javascript that im using to try and access an element on the main page, but it can never seem to find it.</p>

<p>ie:</p>

<p>Here is the .aspx page that loads the iFrame.</p>

<pre><code>&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
        &lt;div&gt;
            &lt;iframe src="myframe.aspx" onload="DoIt();" width="100px" height="100%"&gt;&lt;/iframe&gt;
            &lt;div id='dvT'&gt;hello&lt;/div&gt;    
        &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
</code></pre>

<p>Here is the JS on the iFrame:</p>

<pre><code>&lt;script language="javascript" type="text/javascript"&gt;

    function DoIt() {
        alert(parent.document.getElementById('dvT').innerHTML);
    }

&lt;/script&gt;
</code></pre>

<p>I have tried parent.document, this.document, this.contentWindow.document, etc.</p>

<p>The JS must remain on the page loaded in the iFrame.
Any help is appreciated :-)</p>

<p><hr /></p>

<h2>Here is the solution</h2>

<p>1.) give the iFrame an ID
2.) reference the script in the onload for iFrame via iframeid.methodName
3.) on js in other page, ref via this.parent.document....</p>

<p>Here is the fixed code</p>

<pre><code>&lt;body&gt;
        &lt;form id="form1" runat="server"&gt;
            &lt;div&gt;
                &lt;iframe src="myframe.aspx" onload="myFrame1234.DoIt();" width="100px" height="100%" id="myFrame1234"&gt;&lt;/iframe&gt;
                &lt;div id='dvT'&gt;hello&lt;/div&gt;    
            &lt;/div&gt;
        &lt;/form&gt;
    &lt;/body&gt;

&lt;!--- SCRIPT ON myframe.aspx page ---&gt;
&lt;script language="javascript" type="text/javascript"&gt;

    function DoIt() {
        alert(this.parent.document.getElementById('dvT').innerHTML);
    }

&lt;/script&gt;
</code></pre>
