Home >Web Front-end >HTML Tutorial >What should I do if the web page is iframed?
Everyone knows that there is a function called "nesting", but the website you have worked so hard to build is used by someone else to use an iframe to put a vest on you and it becomes the content of his page. Maybe you will be very angry and want to stop it. , so what should you do to avoid preventing web pages from being iframed? Here are several methods to teach you how to prevent your website and web pages from being iframed by others.
(1) The first way to prevent the page from being iframed, add the following JS code to the page:
<script type="text/javascript"> if(top.window.location.href!=window.location.href){ top.window.location.href=window.location.href; } </script>
(2) The second way to prevent the page from being iframed, add the following to the page JS code:
<script type="text/javascript"> if(top.location!=self.location){ top.location=self.location; } </script>
It is said that this solution will keep refreshing the page under Firefox. This is because windows.top is the current window under IE, and if there is no windows.top under Firefox, then it is null. Therefore, this js code will be continuously run, which creates an infinite loop and causes the page to be refreshed all the time.
(3) The third way to prevent the page from being iframed is to add the following JS code to the page:
<script type="text/javascript"> if(window!=parent){ parent.navigate(window.location.href); } </script>
In addition, if you use a web program developed with php, you can also use php directly to implement it. To prevent a page from being iframed, add the following to the program code:
if($_server['http_host']!='www.phpernote.com'){ exit('禁止iframe我的页面!囧'); }
The asp language also has its own unique implementation code for prohibiting a page from being iframed, as follows:
host=Request.ServerVariables("SERVER_NAME") if host<>"www.phpernote.com" then response.redirect "http://www.phpernote.com/" end if
Of course, other programming languages can also be used. The effect of preventing the page from being iframed is achieved by obtaining environment variables. I will not give examples one by one here.
When you want to prevent others from iframe your website or webpage, you might as well try the above method, I hope it can help you.
Related recommendations:
How to use the iframe attribute
A case study on how to asynchronously upload an iframe in a php form file
How to prevent web pages from being nested in iframes?
The above is the detailed content of What should I do if the web page is iframed?. For more information, please follow other related articles on the PHP Chinese website!