haddar
haddar

Reputation: 25665

Calling iframe function

Is there a way to call a JavaScript function that is inside an IFrame from the parent?

Upvotes: 3

Views: 3753

Answers (3)

RamboNo5
RamboNo5

Reputation: 2060

As far as I know is this rather complicated. You can set up a Cross Domain Communication Channel. For Facebook applications this kind of thing is quite common. This, of course, only works if both sites play along.

Upvotes: 0

Mutation Person
Mutation Person

Reputation: 30498

Where are you wanting the function to be called against? Moreover, what are you wanting it to do?

When you call it are you wanting it to 1) Operate on items in the container page? 2) Operate on items in the IFrame itself 3) Be a generic function accessible by both container and frame.

As far as I am aware

1) Isn't possible

2) Might be possible, given Paulo Santos' answer.

3) Should put function in a common file.

Upvotes: 0

Paulo Santos
Paulo Santos

Reputation: 11567

That depends on the contents of the IFrame. If the content is from another domain then you're out of luck, as most modern browsers have closed that loophole for cross-site attacks.

However, if the content is from the SAME domain then you can use it as it were simply another method on the IFrame window object. If I recall correctly you can use something like window.frames["_your_IFrame_ID_"].window.functionOrMethodName().

Upvotes: 8

Related Questions