HTML框架(iframe )進行限制。很多人喜歡HTML框架(iframe )來加載他人的網頁,加載他人的服務器寬帶。
要求:
1、防止自己網站被他人iframe 內聯框架使用
2、如果自己網站是可以用iframe 內聯框架
<script type="text/javascript"> if (window!=top) // 判斷當前的window對象是否是top對象 top.location.href = window.location.href; // 如果不是,將top對象的網址自動導向被嵌入網頁的網址 </script>
上的代碼只能實現判斷使用內聯框架,如果自己使用的話,也會被跳轉,那么就有了下面的代碼:
try{ top.location.hostname; if (top.location.hostname != window.location.hostname) { top.location.href =window.location.href; } } catch(e){ top.location.href = window.location.href; }
下面的代碼是案例:
下面是a頁面,嵌套b頁面。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>a頁面</title> <style type="text/css" media="screen"> * { padding: 0; margin: 0; border: none; } </style> </head> <body> <iframe src="http://w3cku.cn/test/b_iframe.html"></iframe> </body> </html>
下面是b頁面:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>我是b頁面</title> <style type="text/css" media="screen"> * { padding: 0; margin: 0; border: none; } </style> </head> <body> <div>我是b頁面</div> <script type="text/javascript"> try{ top.location.hostname; if (top.location.hostname != window.location.hostname) { top.location.href =window.location.href; } } catch(e){ top.location.href = window.location.href; } </script> </body> </html>
案例:
來源於:http://www.ruanyifeng.com/blog/2008/10/anti-frameset_javascript_codes.html