How to prevent the page from opening outside of an iframe?

7

Can not open link in iframe

if (self != top) { top.location.replace(window.location.href) }

I need something in javascript that runs counter to this. I want my URL to work only inside an iframe

    
asked by anonymous 03.02.2016 / 14:50

2 answers

1

Unfortunately there will always be ways to circumvent this type of block mainly with the use of javascript, but for the laymen, simple validation would suffice.

function IsFrame () {
    try {
        return window.self !== window.top;
    } catch (e) {
        return true;
    }
}
    
04.02.2016 / 14:00
1

Use the code below to verify that the window in which the page was loaded is the browser window itself, not an iframe:

if(window==window.top) {
    // Página não está em iframe, portanto lança um erro.
    throw new Error('Esta página somente pode ser carregada em um iframe!');
}
    
04.02.2016 / 18:53