Home > Web Front-end > JS Tutorial > How to Access Parent Iframe from JavaScript?

How to Access Parent Iframe from JavaScript?

Susan Sarandon
Release: 2024-11-06 00:59:02
Original
978 people have browsed it

How to Access Parent Iframe from JavaScript?

Accessing Parent Iframe from JavaScript

In web development, it may be necessary to interact with a parent iframe from a loaded page within it. This can be achieved using JavaScript, allowing you to manipulate or retrieve information from the parent frame.

Let's consider a scenario where multiple iframes with dynamically generated content are loaded. Each iframe may contain the same page, but for specific purposes, it's essential to identify which iframe needs to be closed. To do this, an array is maintained to keep references to these iframes.

To access the parent iframe from the loaded page, you can utilize the following approach:

  1. Assign Unique ID and Name to iframe:
    Set both the id and name attributes of the iframe to the same value.

    <iframe>
    Copy after login
  2. Retrieve Parent Frame from Loaded Page:
    Within the child page (i.e., the page loaded inside the iframe), use the following code to access the parent iframe:

    parent.document.getElementById(window.name);
    Copy after login

By following this method, you can establish a connection between the child page and its parent iframe, enabling you to manipulate the iframe or gather information from it programmatically.

The above is the detailed content of How to Access Parent Iframe from JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template