js實(shí)現(xiàn)防止被iframe的方法
本文實(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)文章
Iframe 自動(dòng)適應(yīng)頁面的高度示例代碼
這篇文章主要介紹了Iframe如何自動(dòng)適應(yīng)頁面的高度,需要的朋友可以參考下2014-02-02利用JavaScript實(shí)現(xiàn)簡(jiǎn)單3D開關(guān)書本模型
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)簡(jiǎn)單3D開關(guān)書本模型的效果,文中的實(shí)現(xiàn)代碼講解的非常詳細(xì),具有一定參考價(jià)值,感興趣的同學(xué)可以動(dòng)手嘗試一下2023-11-11JS實(shí)現(xiàn)選擇TextArea內(nèi)文本的方法
這篇文章主要介紹了JS實(shí)現(xiàn)選擇TextArea內(nèi)文本的方法,涉及javascript針對(duì)頁面TextArea元素焦點(diǎn)設(shè)置及文本獲取的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08JavaScript 數(shù)組去重并統(tǒng)計(jì)重復(fù)元素出現(xiàn)的次數(shù)實(shí)例
下面小編就為大家分享一篇JavaScript 數(shù)組去重并統(tǒng)計(jì)重復(fù)元素出現(xiàn)的次數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12純javascript實(shí)現(xiàn)簡(jiǎn)單下拉刷新功能
這篇文章主要介紹了純javascript實(shí)現(xiàn)簡(jiǎn)單下拉刷新功能,沒有借助任何的框架,十分簡(jiǎn)單實(shí)用,有需要的小伙伴來參考下吧。2015-03-03JS 設(shè)計(jì)模式之:工廠模式定義與實(shí)現(xiàn)方法淺析
這篇文章主要介紹了JS 設(shè)計(jì)模式之:工廠模式,結(jié)合實(shí)例形式分析了JS 工廠模式基本概念、原理、定義、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05