How to prevent the page from opening outside an iframe?

Asked

Viewed 813 times

7

Cannot open link in iframe

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

I need something in javascript that is contrary to this. I want my URL to work only within an iframe

  • Related (opposite) :http://answall.com/questions/108087/howto prevent que-me-site-seja-renderizado-em-um-iframe

  • There’s a way, but there’s always room :\

  • 1

    Something like that: if (self == top) { top.location.replace('http://pagina.que/mostra/o_frame') }

2 answers

1

Unfortunately there will always be ways to circumvent this type of lock mainly with the use of javascript however for laymen the simple validation would be enough.

function IsFrame () {
    try {
        return window.self !== window.top;
    } catch (e) {
        return true;
    }
}

1

Use the code below to check that the window in which the page was loaded is the browser window itself and 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!');
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.