1

I have a website(website a) that has only one Page, which i want to show on a different website(website b) via iframe. But I want to prevent the access to website a.

Basicly what i want to do is this:

if (load website trough iframe == true){
         show website;
    }
else{
         don't open website;
     }

Anyone has a clue where to start to solve this problem?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • There's this: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options – John Feb 26 '18 at 10:06
  • 1
    Obligatory: [We Done Been ... Framed!](https://blog.codinghorror.com/we-done-been-framed/) – deceze Feb 26 '18 at 10:11

2 Answers2

0

Preventing access is a strong word. Here's how to check if you're in a frame or not.

if (window.frameElement) {
  // in frame
}
else {
  // not in frame... redirect to another page?
}
Asken
  • 7,679
  • 10
  • 45
  • 77
0

Browser will not provide access to window.top method. So you can use the following code to detect whether the website is running in an Iframe or Browser

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

Now you can do this:

if (isInIframe()){
    document.write("Show Website");
}
else{
    document.write("Don't Show Website");
}
NIKHIL NEDIYODATH
  • 2,703
  • 5
  • 24
  • 30