防止自己的网站被他人iframe内嵌的方法

前言

防止网页被frameset iframe内嵌,内嵌后自动跳出框架,利用JS和HTTP header来实现。

方法

在网站中加入以下代码即可:


(function(){

/*

* JS禁止网页被iframe或frameset嵌套 检测到被嵌套时自动跳出框架

* 支持IE、Chrome、Firefox、Opera、Safari等所有浏览器

* 于飞SEO

* 将此段代码放置在之间 尽量作为第一段js代码 可以节省渲染时间

*/

var isIframed = false;

try{

var topURL = window.top.location.href;

isIframed = topURL !== window.location.href;

}catch(e){

isIframed = true;

}finally{

if(isIframed){

try{

window.top.location.replace(window.location.href);

}catch(e){

}

}

}

})();

猜你喜欢

转载自blog.csdn.net/q906270629/article/details/129585282
今日推荐