ホームページ  >  記事  >  ウェブフロントエンド  >  iframe_javascript スキルを防ぐための js メソッド

iframe_javascript スキルを防ぐための js メソッド

WBOY
WBOYオリジナル
2016-05-16 15:51:311220ブラウズ

この記事の例では、iframe を防ぐための js メソッドについて説明します。皆さんの参考に共有してください。詳細は以下の通りです。

方法 1:

<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>

方法 2:

<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&#63; :)
this.top.location !== this.location && (this.top.location = this.location);
</script>

この記事が皆様の JavaScript プログラミング設計に役立つことを願っています。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。