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

断了今生、忘了曾经 提交于 2020-04-30 05:17:06

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7266107/best-way-to-determine-whether-a-webpage-is-viewed-in-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!