js实现防止被iframe的方法

2019-12-18,,,,

本文实例讲述了js实现防止iframe的方法。分享给大家供大家参考。具体如下:

方法一:

<script>
// Break out of an iframe, if someone shoves your site
// into one of those silly top-bar URL shortener things.
//
// Passing `this` and re-aliasing as `window` ensures
// that the window object hasn't been overwritten.
//
// Example:
//  var window = 'haha, punked!';
//
// Note: Probably unnecessary, but just for kicks.
(function(window) {
 if (window.location !== window.top.location) {
  window.top.location = window.location;
 }
})(this);
</script>

方法二:

<script>
// A more cryptic one-liner, to awe & impress.
//
// No need to protect `window` since `this` is
// immutable, and at the topmost level means
// `window` anyways. Here, we compare locations
// on the left side of the "&&" and execute the
// code in parenthesis if that condition is
// true (top location isn't iframe location).
//
// Otherwise, nothing happens. It's basically an
// if statement without wrapping curly brackets.
//
// Weird, I know. But pretty cool, right? :)
this.top.location !== this.location && (this.top.location = this.location);
</script>

希望本文所述对大家的javascript程序设计有所帮助。

您可能感兴趣的文章:

  • JavaScript实现防止网页被嵌入Frame框架的代码分享
  • 嵌入式iframe子页面与父页面js通信的方法
  • js防止页面被iframe调用的方法
  • js实现网页防止被iframe框架嵌套及几种location.href的区别
  • javascript 获取iframe里页面中元素值的方法
  • javascript iframe内的函数调用实现方法
  • JS防止网页被嵌入iframe框架的方法分析

《js实现防止被iframe的方法.doc》

下载本文的Word格式文档,以方便收藏与打印。