js實(shí)現(xiàn)防止被iframe的方法
更新時(shí)間:2015年07月03日 15:35:25 作者:不吃皮蛋
這篇文章主要介紹了js實(shí)現(xiàn)防止被iframe的方法,實(shí)例分析了兩種比較常用的javascript防止頁面被iframe的技巧,非常簡單實(shí)用,需要的朋友可以參考下
本文實(shí)例講述了js實(shí)現(xiàn)防止被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>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
js處理php輸出時(shí)間戳對(duì)不上號(hào)的解決方法
JS時(shí)間戳為13位,包含3位毫秒的,而PHP只有10位不包含毫秒的,這就是為什么對(duì)不上號(hào)原因,處理方法如下2014-06-06
詳解JS 比較兩個(gè)Json對(duì)象的值是否相等的實(shí)例
這篇文章主要介紹了JS 比較兩個(gè)Json對(duì)象的值是否相等的實(shí)例,有需要的朋友可以參考一下2013-11-11
解決bootstrap中使用modal加載kindeditor時(shí)彈出層文本框不能輸入的問題
這篇文章主要介紹了解決bootstrap中使用modal加載kindeditor時(shí)彈出層文本框不能輸入的問題,需要的朋友可以參考下2017-06-06
鼠標(biāo)滑過出現(xiàn)預(yù)覽的大圖提示效果
這篇文章主要介紹了如何實(shí)現(xiàn)鼠標(biāo)滑過出現(xiàn)預(yù)覽的大圖提示效果,需要的朋友可以參考下2014-02-02

