Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two scenarios that I can think of where you'd want to use <a href="https://developer.mozilla.org/en/DOM/window.parent" rel="nofollow noreferrer">window.parent</a>. The first is when you have a window open another window using <code>window.open</code>. The other is where the first window uses an <code>iframe</code> to load a page. In the former case, it appears as though you actually want to use <a href="https://developer.mozilla.org/en/DOM/window.opener" rel="nofollow noreferrer">window.opener</a>, as ukostin has said. In the latter case, <code>window.parent</code> works fine. Both methods work properly whether the code is inline or loaded from an external JS file. Here are some tests:</p> <p><strong>POPUP</strong></p> <p>parentWindow.htm:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt;function showMsg(msg){alert(msg);}&lt;/script&gt; &lt;body&gt; &lt;a href="#" onclick="window.open('childWindow.htm','child','width=300,height=100')"&gt;Open&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>externalWindow.js:</p> <pre><code>function showMsgExternal(msg){window.opener.showMsg(msg);} </code></pre> <p>childWindow.htm:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt;function showMsgInline(msg){window.opener.showMsg(msg);}&lt;/script&gt; &lt;script src="externalWindow.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="#" onclick="showMsgInline('inline')"&gt;Inline&lt;/a&gt; &lt;a href="#" onclick="showMsgExternal('external')"&gt;External&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>IFRAME</strong></p> <p>parentFrame.htm:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt;function showMsg(msg){alert(msg);}&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;iframe src="childFrame.htm" width="300" height="100"&gt;&lt;/iframe&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>externalFrame.js:</p> <pre><code>function showMsgExternal(msg){window.parent.showMsg(msg);} </code></pre> <p>childFrame.htm:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt;function showMsgInline(msg){window.parent.showMsg(msg);}&lt;/script&gt; &lt;script src="externalFrame.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="#" onclick="showMsgInline('inline')"&gt;Inline&lt;/a&gt; &lt;a href="#" onclick="showMsgExternal('external')"&gt;External&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload