HTML框架(iframe )限制


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 HTML的內聯框架(iframe) HTML模板與iframe框架 HTML - 框架iframe html框架iframe與frameset的介紹 HTML框架集之Frameset與Iframe簡單應用 html中的框架frameset和frame及iframe HTML框架(frameset框架集和iframe內嵌框架) HTML標簽天天練6--