首页 > 其他分享 >防止网站页面被其他网站iframe引用方法

防止网站页面被其他网站iframe引用方法

时间:2023-03-05 17:02:33浏览次数:30  
标签:origin 网站 top window location iframe self 页面


1、在响应头里加一个X-Frame-Options

其取值有三种,大部分浏览器都支持:

DENY:浏览器拒绝当前页面加载任何Frame页面
SAMEORIGIN:frame页面的地址只能为同源域名下的页面
ALLOW-FROM origin:origin为允许frame加载的页面地址

这样被不同源的页面以iframe包含时就不会显示了

设置方法:

防止网站页面被其他网站iframe引用方法_X-Frame-Op

2、js脚本法:
在自己的页面中写入如下js脚本中的一个即可:

if (window != window.top) {
window.top.location.replace(window.location)
// 这是直接代替外窗,你也可以干别的
}



if (top != self) {
top.location = self.location;
}



if (self == top) {
var theBody = document.getElementsByTagName('body')[0];
theBody.style.display = "block";
} else {
top.location = self.location;
}


标签:origin,网站,top,window,location,iframe,self,页面
From: https://blog.51cto.com/sdwml/6101557

相关文章