• 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

  • 相关阅读:
    wepy框架入门
    认识WebStorm-小程序框架wepy
    列表
    透明盒子
    wepy框架滑动组件使用
    底部导航栏效果
    安装less/sass
    wepy快速入门
    实现返回顶部-wepy小程序-前端梳理
    音乐播放器-图片切换-轮播图效果
  • 原文地址:https://www.cnblogs.com/shiyixirui/p/14856209.html
Copyright © 2020-2023  润新知