Search code examples
javascripthttp-redirectiframecolorbox

want to redirect to another page from colorbox iframe after closing it


Does anyone know how to implement the following JavaScript process? I`m using colorbox to display iframe.

  1. I setted the banner link on colorbox iframe.
  2. I want this iframe to close when the banner is clicked, after that I want the parent window to redirect to another page.

following 2 codes are what I`ve tried:   

1
<div>  
<a href='#' onclick='parent.$.colorbox.close();linkto();'>
<img src="../images/test.png" alt=“#” >
</a>
<script>
function linkto() {
location.href = "https://www.test.com";
}
</script>
</div>

2
<div>  
<a href='#' onclick='parent.$.colorbox.close(); window.location.href ='https://www.test.com;'>
<img src="../images/test.png" alt=“#” >
</a>
</div>

Solution

  • You can use window.top.location.href to target the top most window element and redirect to the specified page. This can be done from within aniframe`.

    HTML:

    <a href='#' onclick='parent.$.colorbox.close(); linkTo();">
    

    JS:

    function linkTo() {
        window.top.location.href = "https://www.test.com";
    }