How do I get the from a child iframe to set on the index.html</a></h1> </div> <div class="grid fw-wrap pb8 mb16 bb bc-black-075"> <div class="grid--cell ws-nowrap mr16 mb8" title="2016-01-12 19:07:53Z"> <span class="fc-light mr2">Asked</span> <time itemprop="dateCreated" datetime="2017-12-08T13:23:24.273" class="fromnow">Dec 08 '17 at 13:23</time> </div> <div class="grid--cell ws-nowrap mr16 mb8"> <span class="fc-light mr2">Active</span> <time class="fromnow" title="2017-12-09T05:09:51.663" datetime="2017-12-09T05:09:51.663">Dec 09 '17 at 05:09</a> </div> <div class="grid--cell ws-nowrap mb8" title="Viewed 2,690 times"> <span class="fc-light mr2">Viewed</span> 2,690 times </div> </div> <div id="mainbar" role="main" aria-label="questions and answers"> <div id="question" class="question" data-questionid="47715287" data-ownerid="9068171" data-score="2"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="47715287"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="2">2</div> <button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4"> <svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg> <div class="js-bookmark-count mt4" data-value=""></div> </button> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Currently I have the following in my <code>index.html</code> file</p> <pre><code>document.title = document.getElementById('title').contentWindow.document.title; </code></pre> <p>But it makes the title blank instead of the title of the iframe.</p> <p>How do I fix this?</p> <p><a class="external-link" href="http://deepmotions.github.io" rel="nofollow noreferrer">Here is my site</a></p></div> <div class="mt24 mb12"> <div class="post-taglist grid gs4 gsy fd-column"> <div class="grid ps-relative"> <a href="../../questions/tagged/javascript" class="post-tag js-gps-track" title="show questions tagged 'javascript'" rel="tag">javascript</a> <a href="../../questions/tagged/html" class="post-tag js-gps-track" title="show questions tagged 'html'" rel="tag">html</a> <a href="../../questions/tagged/iframe" class="post-tag js-gps-track" title="show questions tagged 'iframe'" rel="tag">iframe</a> <a href="../../questions/tagged/title" class="post-tag js-gps-track" title="show questions tagged 'title'" rel="tag">title</a> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card__deleted"> <time class="s-user-card--time" datetime="edited Dec 09 '17 at 05:09">edited Dec 09 '17 at 05:09</time> <div class="s-avatar s-avatar__32 s-user-card--avatar"> </div> <div class="s-user-card--info"></div> </div> </div> <div class="post-signature owner grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Dec 08 '17 at 13:23">asked Dec 08 '17 at 13:23</time> <a href="../../users/9068171/deepmotions" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/9068171.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="DeepMotions" /> </a> <div class="s-user-card--info"> <a href="../../users/9068171/deepmotions" class="s-user-card--link">DeepMotions</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">31</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> <li class="s-award-bling s-award-bling__bronze" title="6 bronze badges">6</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-47715287" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="47715287" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-82390770" class="comment js-comment " data-comment-id="82390770" data-comment-owner-id="8691276" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment82390770_47715287"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">which element is 'title' that you are looking for? GetElementById is used to get a DOM element that have an ID...</span> – <a href="../../users/8691276/calvin-nunes" title="6,376 reputation" class="comment-user ">Calvin Nunes</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/47715287/how-do-i-get-the-title-from-a-child-iframe-to-set-on-the-index-html#comment82390770_47715287"><span title="2017-12-08T13:29:48.053 License: CC BY-SA 3.0" class="relativetime-clean">Dec 08 '17 at 13:29</span></a></span> </div> </div> </li> <li id="comment-82390853" class="comment js-comment " data-comment-id="82390853" data-comment-owner-id="9068171" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment82390853_47715287"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">The iframe in which the title should be has id="title".</span> – <a href="../../users/9068171/deepmotions" title="31 reputation" class="comment-user owner">DeepMotions</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/47715287/how-do-i-get-the-title-from-a-child-iframe-to-set-on-the-index-html#comment82390853_47715287"><span title="2017-12-08T13:32:16.040 License: CC BY-SA 3.0" class="relativetime-clean">Dec 08 '17 at 13:32</span></a></span> </div> </div> </li> <li id="comment-82393272" class="comment js-comment " data-comment-id="82393272" data-comment-owner-id="2652134" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment82393272_47715287"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">The code works, where it probably fails is that you're running it before the frame has loaded. See: https://stackoverflow.com/questions/29233928/iframe-onload-javascript-event</span> – <a href="../../users/2652134/brahma-dev" title="1,955 reputation" class="comment-user ">Brahma Dev</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/47715287/how-do-i-get-the-title-from-a-child-iframe-to-set-on-the-index-html#comment82393272_47715287"><span title="2017-12-08T14:41:25.580 License: CC BY-SA 3.0" class="relativetime-clean">Dec 08 '17 at 14:41</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="answers-subheader grid ai-center mb8"> <div class="grid--cell fl1"> <h2 class="mb0" data-answercount="9">1 Answers<span style="display:none;" itemprop="answerCount">1</span></h2> </div> </div> </div> <a name="47717614"></a> <div id="answer-47717614" class="answer accepted-answer" data-answerid="47717614" data-ownerid="6854385" data-score="1" itemprop="acceptedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="47717614"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> <div class="js-accepted-answer-indicator grid--cell fc-green-500 py6 mtn8"><div class="ta-center"><svg aria-hidden="true" class="svg-icon iconCheckmarkLg" width="36" height="36" viewBox="0 0 36 36"><path d="m6 14 8 8L30 6v8L14 30l-8-8v-8z"></path></svg></div></div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Getting the <code>title</code> of an <code>iframe</code> is as easy as:</p> <pre><code>var iframe = document.getElementById("iframeId"); var iframeTitle = iframe.contentDocument.title; </code></pre> <p>If you want to get the value of <code>title</code> when the <code>iframe</code> is loaded, execute:</p> <pre><code>document.getElementById("iframeId").onload = function() { var iframeTitle = document.getElementById("iframeId").contentDocument.title; } </code></pre> <p>The <code>title</code> value is now stored in your <code>iframeTitle</code> variable.</p> <p>Afterwards, you can replace you title value on your current page with this variable.</p> <pre><code>if (document.title != iframeTitle) { document.title = iframeTitle; } </code></pre> <p>If you want to insert this code to your html file, add the following code after your content in your <code><body></code> element.</p> <pre><code><script> document.getElementById("iframeId").onload = function() { var iframeTitle = document.getElementById("iframeId").contentDocument.title; if (document.title != iframeTitle) { document.title = iframeTitle; } } </script> </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2017-12-09T01:52:30.553" class="relativetime">Dec 09 '17 at 01:52</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Felix Haeberle</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Dec 08 '17 at 15:42">answered Dec 08 '17 at 15:42</time> <a href="../../users/6854385/felix-haeberle" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/6854385.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Felix Haeberle" /> </a> <div class="s-user-card--info"> <a href="../../users/6854385/felix-haeberle" class="s-user-card--link">Felix Haeberle</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,530</li> <li class="s-award-bling s-award-bling__silver" title="12 silver badges">12</li> <li class="s-award-bling s-award-bling__bronze" title="19 bronze badges">19</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-47717614" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="47717614" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-82400672" class="comment js-comment " data-comment-id="82400672" data-comment-owner-id="9068171" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment82400672_47717614"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">I'm not so good at Javascript, so where should I put every piece of code.</span> – <a href="../../users/9068171/deepmotions" title="31 reputation" class="comment-user owner">DeepMotions</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/47715287/how-do-i-get-the-title-from-a-child-iframe-to-set-on-the-index-html#comment82400672_47717614"><span title="2017-12-08T18:22:15.640 License: CC BY-SA 3.0" class="relativetime-clean">Dec 08 '17 at 18:22</span></a></span> </div> </div> </li> <li id="comment-82409860" class="comment js-comment " data-comment-id="82409860" data-comment-owner-id="6854385" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment82409860_47717614"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@DeepMotions See my updated code snippet above. Include this code at the bottom of your file before the closing `<p>` tag. If I could help you, it would be nice that you upvote my answer and mark it as the accepted answer.</p></span> – <a href="../../users/6854385/felix-haeberle" title="1,530 reputation" class="comment-user ">Felix Haeberle</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/47715287/how-do-i-get-the-title-from-a-child-iframe-to-set-on-the-index-html#comment82409860_47717614"><span title="2017-12-09T01:54:46.217 License: CC BY-SA 3.0" class="relativetime-clean">Dec 09 '17 at 01:54</span></a></span> </div> </div> </li> <li id="comment-82462234" class="comment js-comment " data-comment-id="82462234" data-comment-owner-id="9068171" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment82462234_47717614"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Nevermind, it didn't work locally, but it did work when hosted.</span> – <a href="../../users/9068171/deepmotions" title="31 reputation" class="comment-user owner">DeepMotions</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/47715287/how-do-i-get-the-title-from-a-child-iframe-to-set-on-the-index-html#comment82462234_47717614"><span title="2017-12-11T10:39:09.110 License: CC BY-SA 3.0" class="relativetime-clean">Dec 11 '17 at 10:39</span></a></span> </div> </div> </li> <li id="comment-82462383" class="comment js-comment " data-comment-id="82462383" data-comment-owner-id="6854385" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment82462383_47717614"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">I strongly recommend to use and learn how the debugger especially for JS work in the browser. Such issues are easily fixed by the help of an debugger. Here is a [link](https://developers.google.com/web/tools/chrome-devtools/) for the use of the chrome debugger. Have a nice day! PS: I like your music:)</span> – <a href="../../users/6854385/felix-haeberle" title="1,530 reputation" class="comment-user ">Felix Haeberle</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/47715287/how-do-i-get-the-title-from-a-child-iframe-to-set-on-the-index-html#comment82462383_47717614"><span title="2017-12-11T10:42:59.710 License: CC BY-SA 3.0" class="relativetime-clean">Dec 11 '17 at 10:42</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>