1品腹、jsonp
<script>
function getWeather(json) {
console.log(json);
}
</script>
<script src="http://e.163.com/jnewsWeather.json? callback=getWeather&cityId=101010100">
</script>
2岖食、postMessage
<iframe id="iframe" width="480px" height="270px" src="http://jasonkid.github.io/fezone/"></iframe>
<script>
var iframe = document.getElementById('iframe');
window.addEventListener('message', function(event) {
console.log(event);
event.source.postMessage('Message from parent page.', '*');
});
</script>
3、iframe + document.name
<iframe id="iframe" width="480px" height="270px" src="http://jasonkid.github.io/fezone/"></iframe>
<script>
var iframe = document.getElementById('iframe');
iframe.onload = function(){
iframe.onload = function(){
console.log(iframe.contentWindow.name);
}
iframe.src = "about:blank";
}
</script>
4舞吭、document.domain
index.html
<iframe id="iframe" width="480px" height="270px" src="iframe.html"></iframe>
<script>
/**
* 在框架的父頁面也要設(shè)置 document.domain
*/
// document.domain = x.x; // 替換為x.x之類的一級域名泡垃,但必須是當(dāng)前domain的后綴。
var iframe = document.getElementById('iframe');
iframe.onload = function(){
iframe.contentDocument.getElementsByTagName('body')[0].innerText = 'Text Changed!';
iframe.contentWindow.iframeFunc();
}
</script>
iframe.html
<p>這是iFrame</p>
<script>
/**
* document.domain 默認(rèn)是整個域名
* 所以不同 N 級域名的 iframe 是無法被獲取的
* 我們將 iframe 的 document.domain 設(shè)置為域名的一級域名
*/
// document.domain = x.x; // 替換為x.x之類的一級域名羡鸥,但必須是當(dāng)前domain的后綴蔑穴。
function iframeFunc() {
console.log('Calling function successfully!');
}
</script>