PHP “header (location)” inside IFRAME, to load in _top location?

前端 未结 8 1764
名媛妹妹
名媛妹妹 2020-12-03 10:20

I have a simple form which is inside IFRAME. When user click on SUBMIT, it redirects to a specific page on my server. The function I use for the redirect is



        
相关标签:
8条回答
  • 2020-12-03 10:36

    In the page that you want to load in _top, add the follow code in the header

    <script type="text/javascript">
    if (top != self) top.location.href = location.href;
    </script>
    
    0 讨论(0)
  • 2020-12-03 10:39

    You are not able to achieve the desired effect in PHP. This is something you'd have to do from JavaScript or add target attribute to <form>:

    <form ... target="_top">
    
    0 讨论(0)
  • 2020-12-03 10:44

    You can either link to the page using <a href="pagename.php?Break=Y">Break Out</a> or use code

    <?php header("Location: pagename.php?Break=Y"); ?>
    

    You then use the following code in the header of the page with

    if(isset($_GET['Break'])) // 
        {
        $BreakFrame = true;
        $BreakToPage = "pagename.php";
    
        }
    
    <script language="JavaScript" type="text/javascript">
    function changeURL( url ) {
        document.location = url;
    }
    </script>
    
    <?php if($BreakFrame) { ?>
    
    <script language="JavaScript" type="text/javascript">
    parent.changeURL('<?=$BreakToPage?>' );
    </script>
    
    <? }?>
    
    0 讨论(0)
  • 2020-12-03 10:46

    A simple way directly in PHP to redirect the parent page rather than the iframe:

    echo "<script>top.window.location = '/mynewpage.php'</script>";
    die;
    

    The die; isn't necessarily necessary, but it is good "just in case" to prevent the script from continuing any further, for example, if javascript is disabled in the user's browser.

    0 讨论(0)
  • 2020-12-03 10:49

    For CakePHP 4, to redirect your parent page just add option 'target'=>'_top' in your iframe's link:

    Example:

     <?= $this->Html->link(__('Redirect Parent'), ['controller' => 'Users', 'action' => 'view'], ['target'=>'_top']) ?>
    

    All the best!

    0 讨论(0)
  • 2020-12-03 10:51

    we can use javascript like this :

    target.window.location='locationpage.html';
    top.window.location='mypage2.html';
    
    0 讨论(0)
提交回复
热议问题