为了避免XRS,我会检查我的网站是在主窗口还是在框架或iframe中加载。是否可以从请求的服务器端检查浏览器地址栏或框架或iframe的src属性。如果无法在服务器端进行检查,则如何使用jQuery进行检查?谢谢帮助。
请使用框架杀手。
CSS:
<style>
html{display:none;}
</style>
Javascript:
<script>
function frameKiller() {
if (self == top) {
document.documentElement.style.display = 'block';
} else {
top.location = self.location;
}
}
</script>
接下来在需要保护的页面上调用framekiller函数
Jquery:
$(function () {
frameKiller();
});
完整示例:
<!DOCTYPE html>
<head>
<title>Page Title</title>
<style>
html{display:none;}
</style>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
Page Content
<script>
function frameKiller() {
if (self == top) {
document.documentElement.style.display = 'block';
} else {
top.location = self.location;
}
}
$(function () {
frameKiller();
});
</script>
</body>
</html>
为了测试,尝试从不同的域加载此页面到一个 iframe 中。