DIV CSS 佈局教程網

location.href 跨視窗與跨 iframe 調用函式完全整理
編輯:關於JavaScript     

location.href 最常用來做跳轉,但很多人一碰到 iframe 巢狀結構就開始混淆:parent、top、self、frames 到底差在哪?本文用三個網頁把這些關係一次講清楚,之後就不會再搞混。

先複習 location 的屬性

location 同時是 window 與 document 的屬性,底下這些成員很常用:

hash:# 符號後面的內容
host:主機名稱加埠號
hostname:主機名稱
href:目前文件的完整 URL,可讀可寫
pathname:網域之後的路徑部分
port:埠號
protocol:通訊協定
search:查詢字串
assign():載入新文件
replace():載入新文件,但不留下歷史紀錄
reload():重新載入
toString():回傳目前 href 的值

3.html —— 最內層的子視窗

<html>
<head>
<title>js</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
  $("#parent").click(function(){
    parent.location.href = "https://www.jb51.net/article/97882.htm";  // 父視窗被跳轉
  })
  $("#top").click(function(){
    top.location.href = "https://www.jb51.net/article/97882.htm";     // 最外層被跳轉
  })
  $("#self").click(function(){
    self.location.href = "https://www.jb51.net/article/97882.htm";    // 自己跳轉
  })
  $("#parentparent").click(function(){
    parent.parent.location.href = "https://www.jb51.net/article/97882.htm"; // 爺視窗
  })
})
function ParentRun() { alert("兒子 iframe 方法!"); }
</script>
</head>
<body>
我是兒子!
<input type="button" id="parent" value="parent.location.href" />
<input type="button" id="top" value="top.location.href" />
<input type="button" id="self" value="self.location.href" />
<input type="button" id="parentparent" value="parent.parent.location.href" />
</body>
</html>

2.html —— 父視窗

<html>
<head>
<title>js</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
  $("#Outermost").click(function(){
    // 判斷自己是不是最外層
    if (top.location == self.location) {
      alert("本 iframe 是最外層框架");
    } else {
      alert("本 iframe 不是最外層框架");
    }
  })
  $("#Son").click(function(){
    window.frames["Son"].location = "https://www.jb51.net/article/97882.htm";
  })
  $("#SonFunction").click(function(){
    window.frames["Son"].ParentRun();   // 呼叫子視窗的函式
  })
  $("#ParentFunction").click(function(){
    parent.SonRun();                    // 呼叫父視窗的函式
  })
})
</script>
</head>
<body>
我是父親!
<iframe src="3.html" name="Son" style="width:300px; height:300px;"></iframe>
<input type="button" id="Outermost" value="判斷是否最外層" />
<input type="button" id="Son" value="控制兒子 iframe 跳轉" />
<input type="button" id="SonFunction" value="呼叫子視窗函式">
<input type="button" id="ParentFunction" value="呼叫父視窗函式">
</body>
</html>

1.html —— 最外層的爺視窗

<html>
<head>
<title>js</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
  alert(window.location == document.location);   // true
})
function SonRun() { alert("爺爺 iframe 方法!"); }

// https://localhost:666/1.html?id=1&name=%E5%BC%A0%E4%B8%89#menu
document.write(location.hash + "<br/>");      // #menu
document.write(location.host + "<br/>");      // localhost:666
document.write(location.hostname + "<br/>");  // localhost
document.write(location.pathname + "<br/>");  // /1.html
document.write(location.port + "<br/>");      // 666
document.write(location.protocol + "<br/>");  // http:
document.write(location.search + "<br/>");    // ?id=1&name=%E5%BC%A0%E4%B8%89
document.write(location.assign + "<br/>");    // function () { [native code] }
</script>
</head>
<body>
我是最外層!
<iframe src="2.html" style="width:500px; height:500px;"></iframe>
</body>
</html>

把三個檔案放在同一個目錄,點幾下按鈕就全懂了。

幾個關鍵觀念

self 永遠等於 window,指的是「目前這個視窗」。parent 是上一層;如果目前已經是最外層,parent 會等於自己。top 則直接跳到最外層,中間不管隔了幾層。

因此 top.location == self.location 就是判斷「我是不是最外層」的標準寫法——只有在最外層時這兩者才相等。

跨 iframe 呼叫函式則是透過 frames 集合,用 name 或索引取得子視窗物件後直接呼叫,例如 window.frames["Son"].ParentRun()。要注意的是這類跨框架操作有 Same Origin 限制,必須同網域才行;用 file:// 直接開檔測試時,部分瀏覽器會擋下來,這是安全性設計而非程式碼寫錯。

最後,jQuery 操作 iframe 內部元素的方式是 $("iframe").contents().find("#id1"),同樣受同源性規範約束。

以上就是 location 相關屬性與跨視窗調用的完整整理,三頁範例搭配起來看,之後遇到多層 iframe 就不會再混淆了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved