Home >Web Front-end >JS Tutorial >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:
Assign Unique ID and Name to iframe:
Set both the id and name attributes of the iframe to the same value.
<iframe>
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);
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!