top.location.href和localtion.href有什麼不同

來源:互聯網
上載者:User

標籤:js ifram top.location.href和localtion.href

top.location.href=”url”  在頂層頁面開啟url(跳出架構)

self.location.href=”url”  僅在本頁面開啟url地址   

parent.location.href=”url” 在父視窗開啟Url地址   

this.location.href=”url”  用法和self的用法一致      

if (top.location == self.location) 判斷當前location 是否為頂層來 禁止frame引用

如果頁面當中有自訂的frame的話,也可以將parent  self   top換為自訂frame的名稱效果就是在自訂frame視窗開啟url地址



實際中可能這樣使用


if(top !== self){ 

    top.location.href = location.href;

}   禁止frame引用


 


以下是從網上找到的一個例子,不是很直觀, 我加了上面那三行代碼, 可以先去掉, 再加上, 看一下效果,就很清楚了 以下是top.htm 代碼

<script language=javascript> function rs(){

if(top !== self){

    top.location.href = location.href;         

}

parent.left.location.href="top.htm"

parent.bot.location.href="top.htm" 

} 

< /script> 

 input type=button name=name value="ksdj" onclick=rs();>

以下是一個隨意檔案名稱的htm檔案: <FRAMESET COLS="150,*"> < FRAME SRC="left.htm" name=left> < FRAMESET ROWS="150,*"> < FRAME SRC="top.htm" name=top> < FRAME SRC="bot.htm" name=bot> < /FRAMESET> < /FRAMESET> 你自己試試,我想你要的可能就是這樣的效果!


top表示主視窗,location表示當前視窗,如果你的檔案只有一個架構,沒有iframe和frmaeset,那麼是完全一致的,沒有區別。


top.location是在頂層frame中開啟新頁   window.location是在當前frame中開啟新頁

parent.location 在當前視窗的父視窗開啟Url地址

 

 

top表示主視窗,location表示當前視窗,如果你的檔案只有一個架構,沒有iframe和frmaeset,那麼是完全一致的,沒有區別。

 


top.location.href和localtion.href有什麼不同

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.