zoukankan      html  css  js  c++  java
  • js小demo-迫使页面总是单独显示,不能被嵌入到iframe中

    有时候我们的网页会被别人内嵌别人的网页 iframe 中,我们只需要在页面中增加以下js就可以让我们的页面内容单独显示出来,不被嵌入到 iframe中

    核心JS代码

    <script>
        if(top.location != self.location){         // 检查当前页面是否处于浏览器顶层
            top.location.replace(self.location);   // 将浏览器顶层页面内容替换为当前页面内容
        }
    </script>

    完整示例:

    a.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        我是外层网页
        <iframe src="./1.html" frameborder="0"></iframe>
    </body>
    </html>

    1.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        我是内嵌网页
        <script>
            if(top.location != self.location){         // 检查当前页面是否处理浏览器顶层
                top.location.replace(self.location);   // 将浏览器顶层页面内容替换为当前页面内容
            }
        </script>
    </body>
    </html>

    在浏览器中打开 a.html,后的结果:--会直接打开1.html

  • 相关阅读:
    单例模式
    面向对象编程(一)
    杨辉三角形
    静态方法,Arrays类,二维数组
    数组,查找算法,二分查找法,冒泡排序,选择排序,插入排序
    万年历(二)
    循环结构
    万年历(一)
    条件结构
    类型转换,位运算符
  • 原文地址:https://www.cnblogs.com/shiyixirui/p/14856209.html
Copyright © 2011-2022 走看看