How to close an iframe within iframe itself

It depends on how the iframe window is created. For example, if the iframe window has an ID or name which can be used to identify the iframe, the iframe can be closed from the parent window.

In iframe, the javascript code is:

1
parent.closeIframe();

In parent, the code is:

1
2
3
function closeIframe() {
$('#iframeID').remove();
}

If you don’t know the iframe ID, iframe usually can be closed by clicking anywhere in the parent window. For instance, the iframe opened by thickbox. The javascript code in iframe is:

1
parent.document.elementFromPoint(0, 0).click();
Share