Solving the problem of communication between parent page and IFRAME page through pseudo protocol

Source: Internet
Author: User

We often have the parent page and the IFRAME page operation, such as

<iframe id = "iframe" ></iframe>

The contents of this iframe are JS written. such as the following code

 
1 2 3 4 5 var iframe = document.getElementById ("iframe"), doc = iframe.contentWindow.document; Doc.open (); Doc.write ("---------something------"); Doc.close ();

The code above is right in most cases. But there is a situation, that is, the parent page explicitly written document.domain = "xxx";

In IE series (IE10 not tried) there will be no permissions error. And in Firefox, Chrome is fine.

Why is that? This is an IE bug, that is, the parent page does not explicitly set Document.domain, the IFRAME will default Document.domain and the parent page, which is

Location.host, the parent-child page is able to communicate, that is, the article header example, but when the parent page explicitly set the document.domain= "", iframe inside the page must also be explicitly set document.domain= "xxx", otherwise it is

Do not have access to iframe.contentWindow.document, that is, there is no way to dynamically write content, in fact, you can also let the iframe point to a specific page, this page explicitly set document.domain= "XXX", The square that begins with the article

The way to write, but the problem is that my parent page has a lot of such iframe, the number is unknown (are advertising bit), so also can not pass a specific page.

So the question comes, in this case, we seem to have no way

1. The parent page is set and must be explicitly set Document.domain

2.iframe page content needs JS dynamic generation.

3. No chance to set SRC for IFRAME.

But the above 3 conditions are satisfied, we can solve this kind of problem by pseudo protocol.

iframe.src= "javascript:void (function () {var d=document;d.open ();d. domain= ' xxx;d.write ' ('---something '); D.close ()}) ()) ";

This way you can explicitly set the document.domain of an IFRAME in line with the parent page.

After writing this, it is true that the requirement of dynamically writing the iframe content is realized, but this page will be bounced out, like window.open ();
What is this for? This is also the bug of IE series, that is, the parent page has <base target= "_blank" ></base>, and the content written by the pseudo protocol of IFRAME will pop up the new page like window.open,
However, the <base> of the parent page must be _self, so the target of base can be set to _self before the iframe.src is called, and the target of base will be set to _blank when the content is finished.

This solves the problem.

Although the pseudo protocol can solve this problem, there are also some risks, if not the last resort, do not use this method.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.