We often have operations on parent pages and iframe pages, such as
The content inside this iframe is written in js. Such as the following code
var iframe = document.getElementById("iframe"), doc = iframe.contentWindow.document; doc.open(); doc.write("---------something------"); doc.close();
The above code is correct in most cases. But there is a situation where the parent page explicitly writes document.domain = "xxx";
In IE series (I haven’t tried it on IE10), there will be an error of not having permission. There is no problem in firefox and chrome.
Why is this? This is a bug in IE, that is, when the parent page does not explicitly set document.domain, the iframe will default to the document.domain being the same as the parent page, that is, both are
location.host, parent and child pages can communicate, that is, the example of the article header, but when the parent page explicitly sets document.domain="", the pages in the iframe must also explicitly set document.domain= "xxx", otherwise
If you don’t have permission to get iframe.contentWindow.document, you can’t write content dynamically. In fact, you can also make the iframe point to a specific page. This page explicitly sets document.domain="xxx", and then through the article The starting square
, but the problem is that my parent page has many such iframes, and the number is unknown (all are advertising spaces), so it cannot pass a specific page.
The problem arises. In this case, we seem to have no solution
1. The parent page is set and must explicitly set document.domain
2. The content of the iframe page needs to be dynamically generated by js.
3. No chance to set src for iframe.
But when the above three conditions are met, we can solve this kind of problem through pseudo-protocol.
In this way, the document.domain of the iframe can be explicitly set to be consistent with the parent page.
After writing this, the need to dynamically write iframe content is indeed realized, but this page will pop up separately, like window.open();
Why is this? This is also a bug in the IE series, that is, the parent page has
But the
This solves the problem.
Although pseudo-protocols can solve this problem, there are some risks. Don’t use this method casually unless it is absolutely necessary.