Links on iframe create another view of the page in IFrame

advertisements

I created a html and used it as a source of an iframe.

In iframe, I added a link on div (See sample code below)

<div onclick="location.href='/cybersecurity/risk-management/index.cfm'; location.href='_parent';" >
    <b>Sample Text</b><br />
    <span>
        Sample Text
    </span>
</div>

When I tried to click the div inside the page of iframe. It creates another view of the page inside the iframe. But for anchors in the iframe works fine, <a href="linkhere" target="_parent" >This is an anchor!</a>. How can I add JavaScript on onClick of my div?

I also tried to use <base target="_parent" />, yet it doesn't work.


Instead of setting the window.location.href, which is the current window (the iframe), you want to set the location of the top most window (the parent).

window.top.location.href = 'foobar'

You can also use window.parent which will use the immediate parent window of the current one, but in most cases it's safer to use window.topsince you actually want to top most parent window.