js實現(xiàn)防止被iframe的方法
更新時間:2015年07月03日 15:35:25 作者:不吃皮蛋
這篇文章主要介紹了js實現(xiàn)防止被iframe的方法,實例分析了兩種比較常用的javascript防止頁面被iframe的技巧,非常簡單實用,需要的朋友可以參考下
本文實例講述了js實現(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>
希望本文所述對大家的javascript程序設(shè)計有所幫助。
相關(guān)文章
利用JavaScript實現(xiàn)簡單3D開關(guān)書本模型
這篇文章主要為大家詳細介紹了如何利用JavaScript實現(xiàn)簡單3D開關(guān)書本模型的效果,文中的實現(xiàn)代碼講解的非常詳細,具有一定參考價值,感興趣的同學可以動手嘗試一下2023-11-11
JS實現(xiàn)選擇TextArea內(nèi)文本的方法
這篇文章主要介紹了JS實現(xiàn)選擇TextArea內(nèi)文本的方法,涉及javascript針對頁面TextArea元素焦點設(shè)置及文本獲取的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
JavaScript 數(shù)組去重并統(tǒng)計重復元素出現(xiàn)的次數(shù)實例
下面小編就為大家分享一篇JavaScript 數(shù)組去重并統(tǒng)計重復元素出現(xiàn)的次數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
JS 設(shè)計模式之:工廠模式定義與實現(xiàn)方法淺析
這篇文章主要介紹了JS 設(shè)計模式之:工廠模式,結(jié)合實例形式分析了JS 工廠模式基本概念、原理、定義、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2020-05-05

