How to detect if my iFrame is used by another website?
How to detect if my iFrame is used by another website?
I make web games. I upload them to portals like Newgrounds. They are being embedded as iFrames on those sites. However, I noticed that other sites "steal" the games by embedding the iFrames on their own websites. Is there any code I can run in my iFrame to detect which site I am embedded on?
I keep searching and a lot of threads say something like
if (window.top.location.hostname != "myhost.com")
console.log("Not on myhost");
but that doesn't seem to work these days due to DOMExceptions. Anything else I can do? Or did I miss something obvious here?
1 Answer
1
First, you can simply tell if the page is embedded in an iframe by checking window.top
itself:
window.top
if (window != window.top)
console.log("In an iframe");
I think the code you posted should only get an exception if the parent page is in a different domain. So you could just check for the error:
try
var = window.top.location.hostname;
catch (e)
console.log("In an iframe from another domain");
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
My game is posted as an iFrame in both cases. Also, Newgrounds seems to host the game on a server with a different domain than their frontend. So sadly, neither of those tests will distinguish between the original and fraudulent website.
– Krystian
Sep 18 '18 at 0:59