Redirect parent window from an iframe action

What JavaScript do I need to use to redirect a parent window from an iframe?

I want them to click a hyperlink which, using JavaScript or any other method, would redirect the parent window to a new URL.

Related posts

14 comments

  1. window.top.location.href = "http://www.example.com"; 
    

    Will redirect the top most parent Iframe.

    window.parent.location.href = "http://www.example.com"; 
    

    Will redirect the parent iframe.

  2. window.top.location.href = "http://example.com";
    

    window.top refers to the window object of the page at the top of the frames hierarchy.

  3. or an alternative is the following (using document object)

    parent.document.location.href = "http://example.com";
    
  4. This will solve the misery.

    <script>parent.location='http://google.com';</script>
    
  5. If you’d like to redirect to another domain without the user having to do anything you can use a link with the property:

    target="_parent"
    

    as said previously, and then use:

    document.getElementById('link').click();
    

    to have it automatically redirect.

    Example:

    <!DOCTYPE HTML>
    
    <html>
    
    <head>
    
    </head>
    
    <body>
    
    <a id="link" target="_parent" href="outsideDomain.html"></a>
    
    <script type="text/javascript">
        document.getElementById('link').click();
    </script>
    
    </body>
    
    </html>
    

    Note: The javascript click() command must come after you declare the link.

  6. For current page – window.location.href = “Your url here”;

    For Parent page – window.top.location.href = “Your url here”;

    From HTML

    <a href="http://someurl" target="_top">link</a>
    
  7. It is possible to redirect from an iframe, but not to get information from the parent.

  8. window.top.location.href = 'index.html';
    

    This will redirect the main window to the index page.
    Thanks

  9. Redirect iframe in parent window by iframe in the same parent:

    window.parent.document.getElementById("content").src = "content.aspx?id=12";
    

Comments are closed.