首页 > Web开发 > 详细

js小demo-迫使页面总是单独显示,不能被嵌入到iframe中

时间:2021-06-06 21:28:06      阅读:30      评论:0      收藏:0      [点我收藏+]

有时候我们的网页会被别人内嵌别人的网页 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

技术分享图片

 

js小demo-迫使页面总是单独显示,不能被嵌入到iframe中

原文:https://www.cnblogs.com/shiyixirui/p/14856209.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!