Maison  >  Article  >  interface Web  >  méthode js pour empêcher les compétences iframe_javascript

méthode js pour empêcher les compétences iframe_javascript

WBOY
WBOYoriginal
2016-05-16 15:51:311220parcourir

L'exemple de cet article décrit la méthode js pour éviter d'être iframe. Partagez-le avec tout le monde pour votre référence. Les détails sont les suivants :

Méthode 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>

Méthode 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>

J'espère que cet article sera utile à la conception de la programmation JavaScript de chacun.

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn