How to understand if a page is loaded as an iframe (external site) using javascript?

I am trying to find a reliable solution to determine if my page was loaded in an iframe on an external site using javascript.

If iframe src is in the same domain, it is easy to cross: you can use if (window.frameElement) or if (window.location! == window.parent.location), but if modern ifre src is not in the same domain throw an exception when accessing the parent object and even compare it to null.

I am completely lost.

+1
source share
3 answers

You might be interested in recording Coding Horror: We did Been Framed . Offers an interesting technical discussion on this topic, including solutions and staffing.

+2
source

You can wrap your check (window.location! == window.parent.location / window.frameElement) in a try-catch statement that you can use to handle the exception yourself.

0
source

Why don't you try the function with the following

var element = window.parent.document if (element) return "In the parent window," else "is not in the parent window";

0
source

All Articles