欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript?跳出iframe框架示例詳解

 更新時(shí)間:2023年11月06日 11:38:40   作者:后除  
這篇文章主要為大家介紹了JavaScript跳出iframe框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、window.top

top 屬性返回最頂層的先輩窗口。該屬性返回對(duì)一個(gè)頂級(jí)窗口的只讀引用。如果窗口本身就是一個(gè)頂級(jí)窗口,top 屬性存放對(duì)窗口自身的引用。如果窗口是一個(gè)框架,那么 top 屬性引用包含框架的頂層窗口。

二、window.self

self 屬性可返回對(duì)窗口自身的只讀引用。等價(jià)于 Window 屬性。

三、window.location

window.location(MDN)對(duì)象用于獲得當(dāng)前頁(yè)面的地址 (URL),并把瀏覽器重定向到新的頁(yè)面。

3.1 示例

window.location = 'https://blog.mazey.net/547.html'; //網(wǎng)站將跳轉(zhuǎn)到后面的網(wǎng)址

3.2 屬性

  • location.hostname 返回 web 主機(jī)的域名
  • location.pathname 返回當(dāng)前頁(yè)面的路徑和文件名
  • location.port 返回 web 主機(jī)的端口(80 或 443)
  • location.protocol 返回所使用的 web 協(xié)議(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>

總結(jié)

若想頁(yè)面跳出 iframe 在里面加上下面這段代碼即可。

if(window.top !== window.self){ //若自身窗口不等于頂層窗口
    window.top.location.href = window.self.location.href; //頂層窗口跳轉(zhuǎn)到自身窗口
}

以上就是JavaScript 跳出 iframe 框架的詳細(xì)內(nèi)容,更多關(guān)于JavaScript 跳出 iframe 框架的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論