URL | 說明 | 是否跨域 |
http://www.a.com/a.jsjavascript http://www.a.com/b.jshtml |
同protocol,domain,port | 容許 |
http://www.a.com/a/a.jsjava http://www.a.com/b/b.jsajax |
同protocol,domain,port,不一樣文件夾 | 容許 |
http://www.a.com:8000/a.js跨域 http://www.a.com/b.js瀏覽器 |
同protocol,domain,不一樣port(默認port:80) | 不容許 |
http://www.a.com/a.js安全 https://www.a.com/b.jsapp |
同domain,port,不一樣protocol | 不容許 |
http://www.a.com/a.jsdom http://192.168.82.45/b.js函數 |
同protocol,port,不一樣domain | 不容許 |
http://www.a.com/a.js http://child.a.com/b.js |
主域相同(a.com),子域不一樣(www,child) | 不容許 |
http://www.a.com/a.js http://a.com/b.js |
一級域名相同,二級域名不一樣(同上) | 不容許 |
http://www.a.com/a.js http://www.b.com/b.js |
同protocol,port,不一樣domian | 不容許 |
<!-- morningstar.com/parent.html --> <iframe id="ifr" src="http://test.morningstar.com/MarketBarometer/html/test.html" width="200px"></iframe> <script>document.domain = 'morningstar.com'; functionaa(str){ console.log(str); } window.onload = function(){ document.getElementById('ifr').contentWindow.bb('aaa'); } </script> <!--test.morningstar.com/test.html --><script>document.domain = 'morningstar.com'; functionbb(str){ console.log(str); } parent.aa('bbb'); </script>
3.經過HTML5 postMessage
.postMessage(message,targetOrigin)參數說明
message: 是要發送的消息,類型爲 String、Object (IE八、9 不支持)
targetOrigin: 是限定消息接收範圍,不限制請使用 '*'
'message',function(e)回調函數第一個參數接受Event對象,有三個經常使用屬性:
data: 消息
origin: 消息來源地址
source: 源 DOMWindow 對象
一個簡單的父頁面qsstage.morningstar.com/parent.html 和子頁面 test.com/test.html創建通訊
<!-- qsstage.morningstar.com.com/parent.html --><iframeid="ifr"src="http://bar.com/b.html"></iframe><script>window.onload = function(){
var ifr = document.querySelector('#ifr');
ifr.contentWindow.postMessage({a: 1}, '*');
}
window.addEventListener('message', function(e){
console.log('bar say: '+e.data);
}, false);
</script>
<!-- test.com/test.html -->
window.addEventListener('message', function(e){
console.log('foo say: ' + e.data.a);
e.source.postMessage('get', '*');
}, false)
一個簡單的父頁面chart.com/parent.html 和子頁面 test.com/child.html創建通訊,經過chart.com/poxy.html實現跨域訪問
<!-- chart.com/parent.html --> <iframe id="test1" src="http://test.com/test.html" width="100%" height="200px"></iframe> <script> function callback(data) { console.log(data); } </script>
<!-- chart.com/poxy.html --> <script type="text/javascript"> window.onload = function () { var data = location.hash.substr(1); data = eval("(" + decodeURIComponent(data) + ")"); top.document.getElementById("test1").style.height = data.height + 'px'; //調用父頁面方法,可不寫 top.callback(data); } </script>
<!-- test.com/child.html --> <div style="height:400px"> <p>我是子頁面</p> </div> <script type="text/javascript"> window.onload = function () { if (!document.getElementById("crossFrame")) { var iframe = document.createElement('iframe'); iframe.setAttribute('style', 'width:100%'); iframe.setAttribute('src', 'http://chart.com/poxy.html'); var height = document.documentElement.scrollHeight; var data = '{height:' + height + '}'; //經過參數傳遞高度heights iframe.src = 'http://chart.com/poxy.html#' + data; document.body.appendChild(iframe); } else { document.getElementById("crossFrame").src = url; } } </script>