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



浙公网安备 33010602011771号