Calling JavaScript Function in Iframe

Calling javascript function in iframe

Use:

document.getElementById("resultFrame").contentWindow.Reset();

to access the Reset function in the iframe

document.getElementById("resultFrame")
will get the iframe in your code, and contentWindow will get the window object in the iframe. Once you have the child window, you can refer to javascript in that context.

Also see HERE in particular the answer from bobince.

Invoking JavaScript code in an iframe from the parent page

Assume your iFrame's id is "targetFrame" and the function you want to call is targetFunction():

document.getElementById('targetFrame').contentWindow.targetFunction();

You can also access the frame using window.frames instead of document.getElementById.

// this option does not work in most of latest versions of chrome and Firefox
window.frames[0].frameElement.contentWindow.targetFunction();

Call JavaScript function inside an different iframe

Found a way to do and not using postMessage API

On caller iframe (iframe2), I call an JS function located at parent document

onclick="changeCenter({lat:-20.419528,lng:-54.576573})"

On parent has a function that recalls a function inside iframe1 passing parameter

function MapCenter(center) 
{
document.getElementById("IFRAME1").contentWindow.changeCenter(center);
}

Finally inside iframe1 is the function that will execute the action on iframe1

function changeCenter(center) 
{
map.setZoom(15);
map.setCenter(center);
}

Calling javascript function in iframe

Use:

document.getElementById("resultFrame").contentWindow.Reset();

to access the Reset function in the iframe

document.getElementById("resultFrame")
will get the iframe in your code, and contentWindow will get the window object in the iframe. Once you have the child window, you can refer to javascript in that context.

Also see HERE in particular the answer from bobince.

Call a javascript function from an iframe

You can use window.parent to access the containing page:

window.parent.someToggleFunc();

Note that Javascript access between iframes like this are subject to the Same Origin Policy.



Related Topics



Leave a reply



Submit