Home > Web Front-end > JS Tutorial > How Can I Call a JavaScript Function Inside an iFrame from its Parent Page?

How Can I Call a JavaScript Function Inside an iFrame from its Parent Page?

DDD
Release: 2024-12-11 17:40:22
Original
359 people have browsed it

How Can I Call a JavaScript Function Inside an iFrame from its Parent Page?

Accessing JavaScript Functions Within an iFrame from the Parent Page

To invoke JavaScript code within an iframe from the parent page, first identify the iframe's ID, such as "targetFrame," and the function to call within the iframe, such as "targetFunction()."

Accessing the iframe:

  • Using document.getElementById: document.getElementById('targetFrame').contentWindow.targetFunction();
  • Using window.frames: window.frames[0].frameElement.contentWindow.targetFunction(); (Note: This method may not work in newer versions of Chrome and Firefox.)

Example:

// Target the iframe with ID "targetFrame"
const iframe = document.getElementById('targetFrame');

// Get the window object of the iframe
const iframeWindow = iframe.contentWindow;

// Invoke the "targetFunction()" function within the iframe
iframeWindow.targetFunction();
Copy after login

The above is the detailed content of How Can I Call a JavaScript Function Inside an iFrame from its Parent Page?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template