Summary of how JS operates on pages inside and outside Iframe

Summary of how JS operates on pages inside and outside Iframe

Get the content of the iframe outside the iframe

Method 1

Through the two APIs contentWindow and contentDocument:

var iframe = document.getElementById("iframe1");
var iwindow = iframe.contentWindow;
var idoc = iwindow.document;
var idocument = iframe.contentDocument;
console.log("window",iwindow); //Get the window object of iframe console.log("document",idoc); //Get the document of iframe
console.log("html",idoc.documentElement);//Get the html of the iframe

Among them, iframe.contentWindow can get the window object of iframe, and iframe.contentDocument can get the document object of iframe.

Method 2

Combined with the Name attribute, get it through the frames provided by the window:

<iframe src ="/index.html" id="ifr1" name="ifr2" scrolling="yes">
  <p>Your browser does not support iframes.</p>
</iframe>
<script type="text/javascript">
    console.log(window.frames['ifr2'].window);
    console.dir(document.getElementById("iframe").contentWindow);
</script>

Get content outside the iframe within the iframe

window.parent gets the window object of the previous level. If it is still an iframe, it is the window object of the iframe.
window.top gets the window object of the top-level container, that is, the document of the page you opened

Calling methods and variables defined in the parent page in the iframe

window.parent.window.parentMethod();
window.parent.window.parentValue;

Methods and variables for operating iframe child pages in the parent page

window.frames["iframe_Name"].window.childMethod();
window.frames["iframe_Name"].window.childValue;

Summarize

There are two more points to note when using Iframe:

  1. Make sure to perform operations after the iframe has loaded. If you call methods or variables inside the iframe before it has loaded, an error will occur.
  2. If the iframe links to an external page, the security mechanism cannot use the communication method under the same domain name;

Determine if iframe loading is complete

iframe.onload = function() {
    //TODO
}

Different domain communication

Passing data from parent page to child page

Use the hash value of the location object to pass communication data through it. Add a data string after the src of the iframe in the parent page, and then get the data here instantly in some way in the child page.

Subpage passes data to parent page

A proxy iframe is used, which is embedded in the child page and must be in the same domain as the parent page. Then, the implementation principle of the same-domain communication method mentioned above is fully utilized to pass the data of the child page to the proxy iframe. Then, since the proxy iframe and the main page are in the same domain, the main page can obtain these data using the same domain method.

This is the end of this article about how JS can operate on pages inside and outside the Iframe. For more relevant content about JS operating on Iframe pages, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to make if judgment in js as smooth as silk
  • Implementation of a simplified version of JSON.stringify and its six major features explained in detail
  • Summary of various uses of JSON.stringify
  • Vue implements online preview of PDF files (using pdf.js/iframe/embed)
  • Summary of JavaScript JSON.stringify() usage
  • Detailed explanation of how to solve the circular reference problem encountered when using JSON.stringify
  • The difference and use of json.stringify() and json.parse()
  • Selenium+BeautifulSoup+json gets the json data in the Script tag
  • About if contains comma expression in JavaScript

<<:  Markup Languages ​​- Lists Again

>>:  Mysql modify stored procedure related permissions issue

Recommend

Attributes and usage of ins and del tags

ins and del were introduced in HTML 4.0 to help au...

The perfect solution for highlighting keywords in HTML

I recently encountered a feature while working on...

Steps to build a Docker image using Dockerfile

Dockerfile is a text file that contains instructi...

Implementation of multi-port mapping of nginx reverse proxy

Code Explanation 1.1 http:www.baidu.test.com defa...

Summary of knowledge points about covering index in MySQL

If an index contains (or covers) the values ​​of ...

How to convert mysql bin-log log files to sql files

View mysqlbinlog version mysqlbinlog -V [--versio...

js to achieve a simple lottery function

This article shares the specific code of js to im...

Node.js+postman to simulate HTTP server and client interaction

Table of contents 1. Node builds HTTP server 2. H...

How to view and set the mysql time zone

1. Check the database time zone show variables li...

MySQL 8.X installation tutorial under Windows

I had been using MySQL 5.7 before, but because My...

Use of MySQL triggers

Triggers can cause other SQL code to run before o...

Introduction to Linux common hard disk management commands

Table of contents 1. df command 2. du command 3. ...

Docker adds a bridge and sets the IP address range

I don't know if it's because the binary d...

How to manage multiple projects on CentOS SVN server

One demand Generally speaking, a company has mult...

Steps for Vue to use Ref to get components across levels

Vue uses Ref to get component instances across le...