nivanka
nivanka

Reputation: 1372

best way to determine whether a webpage is viewed in an iframe

Is there a way to determine whether the user is using a web page in side and iframe or is it normal browsing using PHP?

Upvotes: 1

Views: 1410

Answers (3)

PatS
PatS

Reputation: 11474

The solution is to see if the parent's location and the current window's location is the same. If it is the same, then the page was loaded normally, if it is different then the page was loaded in an iframe.

var isInIFrame = (window.location != window.parent.location) ? true : false;

This came from this website. http://www.24hourapps.com/2009/01/check-if-page-is-loaded-in-iframe-using.html, and it came from the SO question here Check if site is inside iframe.

NOTE: In one test, I got a cross browser origin error but that would also only come if the two locations were different.

Upvotes: 0

s.webbandit
s.webbandit

Reputation: 17000

You can add some GET parameters to the request while using IFRAME.

<iframe src="http://www.example.com/iframe?iframe=1">

But while non-iframe request there wouldn't be this GET parameter.

You can check is this GET parameter presents and define it in the session.

So there would be different sessions for iframe and usual window.

Upvotes: 1

lqez
lqez

Reputation: 3008

Using the javascript code that @deceze mentioned above (I pasted it in below),

if (parent.frames.length > 0) { ... }

If the above code noticed the page was displayed within iframe, then call 'IAmInIFRAME.php'(just example) via ajax call.

Upvotes: 2

Related Questions