Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: How to reload the parent frame

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default How to reload the parent frame

    Sample parent code:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Parent</title>
    </head>
    <body>
    <iframe src="https://dl.dropboxusercontent.com/u/4017788/Labs/child.html" width="200" height="100"></iframe>
    </body>
    </html>
    See it in action:
    https://googledrive.com/host/0B5jOXz...m8/parent.html


    Sample child code:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Child</title>
    </head>
    <body>
    <button onclick="myFunction();">Try it!</button>
    <script>
    function myFunction() {
    parent.location.reload();
    }
    </script>
    </body>
    </html>
    I have tried many methods offered in similar questions to no avail, such as:

    window.parent.location.reload();
    top.location.reload();
    etc.

    What am I missing and what's the right approach?

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    If I'm reading it right, you want a button in the framed (child) page to refresh the main framing (parent) page?

    You should be able to do that with a hyperlink to the parent page that has a target of parent;

    In child.html - <a href="parent.html" target="parent">Reload</a>
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by Beverleyh View Post
    If I'm reading it right, you want a button in the framed (child) page to refresh the main framing (parent) page?

    You should be able to do that with a hyperlink to the parent page that has a target of parent;

    In child.html - <a href="parent.html" target="parent">Reload</a>
    The problem is the parent frame URL is not fixed -- it's dynamic.

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    In what way? Can you give examples?

    (This is the type of information you should provide in your opening post)

    Also, are the pages on different domains? Probably why the JavaScript isn't working due to same origin policy.

    Here's something that might offer further guidance: http://www.nczonline.net/blog/2013/0...frames-parent/
    Last edited by Beverleyh; 01-01-2014 at 05:01 PM. Reason: Reference link provided
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there Rain Lover,

    try it like this...
    Code:
    
    <!DOCTYPE html>
    <html>
    
    <head>
    
    <meta charset="UTF-8">
    
    <title>Child</title>
    
    </head>
    <body>
    
    <button id="mybutton">Try it!</button>
    
    <script>
    (function() {
       'use strict';
    
    document.getElementById('mybutton').onclick=function() {
       parent.location.reload(true);
     }
    })();
    </script>
    
    </body>
    </html>
    ...but note that Chrome 31.0 and Opera 18.0 will not render this locally.

    The files must be uploaded to your server.


    coothead
    Last edited by coothead; 01-01-2014 at 06:34 PM. Reason: removed link

  6. #6
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by coothead View Post
    Hi there Rain Lover,

    try it like this...
    Code:
    
    <!DOCTYPE html>
    <html>
    
    <head>
    
    <meta charset="UTF-8">
    
    <title>Child</title>
    
    </head>
    <body>
    
    <button id="mybutton">Try it!</button>
    
    <script>
    (function() {
       'use strict';
    
    document.getElementById('mybutton').onclick=function() {
       parent.location.reload(true);
     }
    })();
    </script>
    
    </body>
    </html>
    ...but note that Chrome 31.0 and Opera 18.0 will not render this locally.

    The files must be uploaded to your server.

    Test it here...

    coothead
    Many thanks for the answer, but it doesn't seem to work:
    https://googledrive.com/host/0B5jOXz...8/parent2.html

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by coothead View Post
    The files must be uploaded to your server.
    And that's probably the problem*. As Beverley already noted, unless both the parent and child pages are on the same domain, security features of browsers will not allow a javascript solution.

    So, if the pages are not both on the same domain, and the 'top' page's URL is dynamic, we would need to know in what way (again as Beverley already noted). However, that might not be enough, but if it's possible would probably hold the key

    *The original code in the first post in this thread should/would work if the pages were both on the same domain.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by jscheuer1 View Post
    So, if the pages are not both on the same domain, and the 'top' page's URL is dynamic, we would need to know in what way (again as Beverley already noted). However, that might not be enough, but if it's possible would probably hold the key
    Dear John,

    Actually I'm working on a Google gadget that should be used on different pages and by different users. Then the parent frame URL depends on the gadget user's website. In short, I cannot count on a fixed parent URL in my coding.

    unless both the parent and child pages are on the same domain, security features of browsers will not allow a javascript solution.
    Then I need to put my question this way: How to reload the parent frame that's located on a different frame? In other words, how can I reload the whole page via the iframe that's on a different domain?

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The way Beverley mentioned in her first post, which brings us back to the question - in just exactly what way is the top URL dynamic? If it is known to the person who is using the widget on their site for instance, it could be passed by them to the widget.

    Like say the top page is mydomain.com/somepage.htm. And the child page, the widget, is googlesomething.com/widget. The url used as the src attribute of the iframe could be:

    googlesomething.com/widget?top=mydomain.com/somepage.htm

    The widget could then use either javascript or server side code to get the mydomain.com/somepage.htm part out of it's location object/URL and use it to load that URL (thus refreshing the top page). In javascript, on the page in the iframe, this information (for a src attribute following the above template) would be:

    Code:
    location.search.split('=')[1]
    This requires either that the person installing the widget on his or her site can add this information manually or that a script they can use to write the iframe tag would include it.

    So you could have on the page in the iframe:

    Code:
    var topURL = location.search.split('=')[1];
    document.write('<a target="_top" href="http://www.' + topURL + '">Refresh</a>');
    That (assuming I've explained this clearly enough and without undue typos) would make a link that, when clicked would reload the top page from the iframe.

    Does that sound workable?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Rain Lover (01-02-2014)

  11. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I tried this out and it would probably be simplest to pass the entire URL via javascript. Like this would be the top page:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <script type="text/javascript">
    document.write('<iframe src="child.htm?top=' + encodeURIComponent(location.href) + '" width="300" height="300" scrolling="auto" frameborder="1"></iframe>');
    </script>
    </body>
    </html>
    Change child.htm to the full path to the widget. Then on the widget page (the page in the iframe) have:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    </head>
    <body>
    <script type="text/javascript">
    var topURL = unescape(location.search.split('=')[1]);
    document.write('<a target="_top" href="' + topURL + '">Refresh</a>');
    </script>
    </body>
    </html>
    That should take care of it. This could also be done using the DOM to create these elements rather than document.write, and that would probably be better.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. DD Modal window v1.1 - close and reload parent
    By kraftworkz in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 10-30-2008, 08:40 PM
  2. Reload selectbox on parent modal from child
    By cnek03 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 07-20-2007, 07:12 PM
  3. How do I reload one frame from another frame?
    By Jim Weinberg in forum JavaScript
    Replies: 2
    Last Post: 01-29-2006, 01:26 PM
  4. How do I reload one frame from another frame?
    By Jim Weinberg in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 01-29-2006, 01:22 AM
  5. Reload external page in frame, no-cache?
    By KE5EHI in forum JavaScript
    Replies: 14
    Last Post: 12-28-2005, 01:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •