JavaScript跳出iframe架構

來源:互聯網
上載者:User

標籤:click   htm   elf   pat   path   add   func   頂層視窗   org   

一、window.top

top屬性返回最頂層的先輩視窗。

該屬性返回對一個最上層視窗的唯讀引用。如果視窗本身就是一個最上層視窗,top屬性存放對視窗自身的引用。如果視窗是一個架構,那麼top屬性引用包含架構的頂層視窗。

二、window.self

self屬性可返回對視窗自身的唯讀引用。等價於Window屬性。

三、window.location

window.location(MDN)對象用於獲得當前頁面的地址 (URL),並把瀏覽器重新導向到新的頁面。

3.1 樣本
window.location = ‘http://www.mazey.net/‘; //網站將跳轉到後面的網址
3.2 屬性
  • location.hostname返回web主機的網域名稱
  • location.pathname返回當前頁面的路徑和檔案名稱
  • location.port返回web主機的連接埠(80或443)
  • location.protocol返回所使用的web協議(http://或https://)
四、iframe樣本
<h1>iframe 1</h1><button type="button" id="self">Show Self</button><button type="button" id="selflocation">Show Self Location</button><button type="button" id="selfhref">Show Self Href</button><button type="button" id="top">Show Top</button><button type="button" id="toplocation">Show Top Location</button><button type="button" id="tophref">Show Top Href</button><button type="button" id="replace">Replace Me To Whole Page</button><script>document.getElementById(‘self‘).addEventListener(‘click‘, function(){    alert(window.self);});document.getElementById(‘selflocation‘).addEventListener(‘click‘, function(){    alert(window.self.location);});document.getElementById(‘selfhref‘).addEventListener(‘click‘, function(){    alert(window.self.location.href);});document.getElementById(‘top‘).addEventListener(‘click‘, function(){    alert(window.top);});document.getElementById(‘toplocation‘).addEventListener(‘click‘, function(){    alert(window.top.location);});document.getElementById(‘tophref‘).addEventListener(‘click‘, function(){    alert(window.top.location.href);});document.getElementById(‘replace‘).addEventListener(‘click‘, function(){    if(window.top !== window.self){        window.top.location.href = window.self.location.href;        console.log(‘You replace successfully!‘);    }else{        console.log(‘You don\‘t need replace, i\‘m top!‘);    }});</script>

五、總結

若想頁面跳出iframe在裡面加上下面這段代碼即可。

if(window.top !== window.self){ //若自身視窗不等於頂層視窗    window.top.location.href = window.self.location.href; //頂層視窗跳轉到自身視窗}

JavaScript跳出iframe架構

JavaScript跳出iframe架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.