JS代码:防止网页被Frame框架调用

2023-05-3112:43:32WEB前端开发Comments914 views字数 1567阅读模式

先看看防止网页被Frame框架调用的JS代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/43740.html

<script language="javascript">
if(window.self != window.top){
    window.top.location.replace(window.self.location);
}
</script>

原理是判断window.self是否等于window.top文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/43740.html

范例代码
example.html文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/43740.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>使用iframe引用网页</title>
</head>
<body>
<iframe src="js-no-iframe.html" style="width:600px;height:350px;"></iframe>
</body>
</html>

js-no-iframe.html文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/43740.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>使用JS防止网页被Frame框架调用</title>
<script language="JavaScript">
//网页如果被iframe引用,用此页强行代替父页
if(window.self != window.top){
    window.top.location.replace(window.self.location);
}
</script>
</head>
<body>
这是被iframe引用的页面
</body>
</html>

破解方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/43740.html

​​​​​​​// 顶层窗口中放入代码 
var location = document.location; 
// 或者 var location = "";

范例代码
example2.html文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/43740.html

​​​​​​​<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>iframe引用网页范例</title>
<script language="javascript">
  var location = document.location; 
</script>
</head>
<body>
<iframe src="js-no-iframe.html" style="width:600px;height:350px;border:1px;"></iframe>
</body>
</html>

从该范例看到,网页并不能被成功引用,所谓的破解代码并没有效果文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/43740.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/43740.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定