Use pseudo-Protocol to solve the problem of communication between parent pages and iframe pages _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to solve the problem of communication between the parent page and the iframe page through pseudo-Protocol. For more information, see the operations on the parent page and the iframe page. For example:

The content in this iframe is written in js. As shown in 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. However, the parent page explicitly writes document. domain = "xxx ";

In the ie series (IE10 has not tried), the error "no permission" will occur. In firefox, chrome is okay.

Why? This is a bug in ie, that is, when the parent page does not explicitly set document. domain, iframe will be consistent with the parent page by default, that is, both

Location. host, the Parent and Child pages can communicate, that is, the example of the article header, but when the parent page explicitly sets document. domain = "", the page in iframe must also explicitly set document. domain = "xxx", otherwise yes

No permission is granted to iframe.content+doc ument, that is, there is no way to dynamically write content. In fact, iframe can also be directed to a specific page, which explicitly sets document. domain = "xxx", and then start

But the problem is that my parent page has many such iframe, and the number is unknown (all advertising spaces), so it cannot be written through a specific page.

This is the problem. In this case, we seem to have no choice.

1. The parent page is set and must explicitly set document. domain

2. The content of the iframe page must be dynamically generated by js.

3. You have no chance to set src for iframe.

However, when all the above three conditions are met, we can solve such problems through pseudo-protocols.

The Code is as follows:


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

In this way, you can explicitly set the document. domain of iframe to be consistent with the parent page.

After writing this code, you do need to dynamically write iframe content, but this page will pop up separately, such as window. open ();
Why? This is also a bug in the ie series, that is, the parent page hasThe content written through the iframe pseudo protocol will pop up a new page like window. open,
HoweverIt must be _ self. Therefore, you can set the target of the base to _ self only before calling iframe. src. After the content is written, set the target of the base to _ blank.

This solves the problem.

Although pseudo-protocols can solve this problem, there are also some risks. If you do not have to, do not use this method.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.