This article mainly introduces how to implement javascript to prevent iframe. The example analyzes two commonly used javascript techniques to prevent page iframe, which is very simple and practical, if you need it, refer to the example in this article to describe how to implement JavaScript to prevent iframe. Share it with you for your reference. The details are as follows:
Method 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》
Method 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? :)this.top.location !== this.location && (this.top.location = this.location);《script》
I hope this article will help you design javascript programs.