Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isInIframe Detection in Javascript

Javascript:

inFrame = true;
try {
   if(window.top == self) {
       inFrame = false;
   }
} catch (err){}

try {
   if(window.parent.location == self.location){
        inFrame = false;    
   }
} catch (err){}

This is a piece of javascript code I use to detect whether my library is inside an iframe. 1 out of 100% of requests report that my library is inside an iframe which I think is not possible.

Is there a possibility that this code to fail [report true on false or vice versa]?

From access log [I log every such iframe requests]

  1. I found that it happens in all modern browsers [ie 6-9, chrome, ff, safari] from user agent string.
  2. I couldn't make any sense out of referrer because they are same as my publisher site.
  3. In some cases I found for same referrer, same url requested & same client ip my library had been once inside an iframe & not in other. This made me doubt the above code.

Will there be any difference while window or document is being loaded in the properties [self, location, top, parent] I use to check? Because my script loads and executes synchronously mostly before the document is ready or window is completely loaded and that does the iniframe test to proceed further.

like image 629
Tamil Avatar asked Nov 05 '22 03:11

Tamil


1 Answers

this is how I get the is in frame result and it works for me inFrame = window.top != window

like image 96
Özgür Kara Avatar answered Nov 07 '22 22:11

Özgür Kara