1. 云栖社区>
  2. >
  3. 正文

js实现防止被iframe的方法_javascript技巧

作者:用户 来源:互联网 时间:2016-07-03 20:25:34

jsiframe防止

js实现防止被iframe的方法_javascript技巧 - 摘要: 本文讲的是js实现防止被iframe的方法_javascript技巧, 本文实例讲述了js实现防止被iframe的方法。分享给大家供大家参考。具体如下: 方法一: <script> // Break out of an iframe, if someone shoves your site /

本文实例讲述了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程序设计有所帮助。

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索js , iframe 防止 javascript iframe、javascript获取iframe、javascript操作iframe、javascript刷新iframe、javascript创建iframe,以便于您获取更多的相关知识。