前面談過用 jsonp 跨域取數據,本文則介紹如何借助 window.name + iframe 完成同樣的事。先來認識兩個關鍵角色。
iframe 是 HTML 的內聯框架標籤,透過 src 屬性即可指定要載入的頁面(HTML、PHP 等)。而 window.name 表面上像個普通全局變數,實際上它是「當前窗口的名字」。要注意的是,每個 iframe 都有一個包裹它的 window,作為頂層窗口的子窗口,自然也擁有 window.name。它最神奇的地方在於:name 值在頁面(甚至跨域名)重新載入後依然存在(若不修改就不會改變),而且能容納極長的字串(可達 2MB)。
解法似乎呼之欲出:在 index.html 中建立一個 iframe,src 指向服務器文件,由服務器把數據寫進 window.name,index 再讀取該 iframe 的 name 值。看似水到渠成:
<body>
<script type="text/javascript">
iframe = document.createElement('iframe'),
iframe.src = 'https://localhost:8080/data.php';
document.body.appendChild(iframe);
iframe.onload = function() {
console.log(iframe.contentWindow.name)
};
</script>
</body>
<?php echo '<script> window.name = "{\"name\":\"hanzichi\", \"age\":10}"; </script>' ?>
然而事與願違,瀏覽器報錯了:協議、主機、端口必須一致——擺明就是跨域。原因在於,若 index.html 與其 iframe 的 src 不同源,就無法操作框架內的任何東西,自然也就讀不到 name。既然要求同源,那就換個 src:利用 name 值不因載入而改變的特性,在 index.html 同目錄下建一個 proxy.html 空頁,於 iframe 載入後迅速把 src 重置為同源位址:
<body>
<script type="text/javascript">
iframe = document.createElement('iframe'),
iframe.src = 'https://localhost:8080/data.php';
document.body.appendChild(iframe);
iframe.onload = function() {
iframe.src = 'https://localhost:81/cross-domain/proxy.html';
console.log(iframe.contentWindow.name)
};
</script>
</body>
理想很美好,現實卻殘酷:每次 onload 觸發後重置 src,等同重新載入,又再次觸發 onload,於是 iframe 陷入無限刷新(所幸數據仍能印出)。要打破迴圈,就得用一個狀態標記來區分「第一次載入」與「重置後的載入」:
<body>
<script type="text/javascript">
iframe = document.createElement('iframe');
iframe.style.display = 'none';
var state = 0;
iframe.onload = function() {
if(state === 1) {
var data = JSON.parse(iframe.contentWindow.name);
console.log(data);
iframe.contentWindow.document.write('');
iframe.contentWindow.close();
document.body.removeChild(iframe);
} else if(state === 0) {
state = 1;
iframe.contentWindow.location = 'https://localhost:81/cross-domain/proxy.html';
}
};
iframe.src = 'https://localhost:8080/data.php';
document.body.appendChild(iframe);
</script>
</body>
順帶補充 window 與 contentWindow 的概念:瀏覽器會為原始文檔建立一個 window 對象,並為每個框架(iframe)建立額外的子 window。關閉原始窗口會連帶關閉所有子窗口;而 contentWindow 屬性指的正是指定 frame 或 iframe 所在的 window 對象。
相比 jsonp,此法無論原理還是用法都更繁瑣。服務端一般輸出一段 JS:
<?php echo '<script> window.name = "{\"name\":\"hanzichi\", \"age\":10}"; </script>' ?>
window.name 的值是字串形式,也就是要傳回客戶端的數據,需要時再自行解析(如字串轉 JSON)。index.html 中可把流程封裝成函數:
<body>
<script type="text/javascript">
function crossDomain(url, fn) {
iframe = document.createElement('iframe');
iframe.style.display = 'none';
var state = 0;
iframe.onload = function() {
if(state === 1) {
fn(iframe.contentWindow.name);
iframe.contentWindow.document.write('');
iframe.contentWindow.close();
document.body.removeChild(iframe);
} else if(state === 0) {
state = 1;
iframe.contentWindow.location = 'https://localhost:81/cross-domain/proxy.html';
}
};
iframe.src = url;
document.body.appendChild(iframe);
}
// 調用
var url = 'https://localhost:8080/data.php';
crossDomain(url, function(data) {
var data = JSON.parse(iframe.contentWindow.name);
console.log(data);
});
</script>
</body>
還有兩點值得留意:一是 IE 的兼容問題(onload 在 IE 下可能需用 attachEvent,JSON 也需 polyfill);二是 proxy 代理頁其實可以不存在,即便回報 404 也不影響功能,只要路徑與 index 同源即可。另外,重置 src 時用 iframe.contentWindow.location = ... 與 iframe.src = ... 都能得到結果,聰明的你不妨想想兩者的差別。