![](/img/trans.png)
[英]Is it possible to get the referrer of an iFrame's parent page with JavaScript in iFrame?
[英]How to get the parent iframe's parent iframe using JavaScript?
所以我有一個帶有iframe的頁面index.html
:
<iframe src="iframe1.html" width="500" height="400"></iframe>
在iframe1.html
我還有另一個嵌套的iframe:
<iframe src="iframe2.html" width="500" height="400"></iframe>
在iframe2.html
我有以下代碼:
<script>
window.onload = function() {
document.getElementById('change-color').onclick = function() {
window.parent.document.body.style.backgroundColor = "red";
return false;
};
}
</script>
<a href="" id="change-color">Change Color</a>
現在,當您單擊“更改顏色”鏈接時,iframe的父頁面iframe1.html
將獲得紅色背景色。 相反,我希望父級的父頁面index.html
獲得顏色更改。 如何訪問?
使用window.parent.parent
獲取祖父母窗口。 所以你要:
window.parent.parent.document.body.style.backgroundColor = "red";
而且,如果您想要最頂層的父級,則無論您有多深,都可以使用window.top
。 看到
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.