user3713442
user3713442

Reputation: 508

Competing javascript onloads in a main window and iframe

I've been reading up on activating javascript when an onload is fired and how to link multiple instances together (and some of the pitfalls), but will there be any "clash" between onloads if I run one in the body of a main parent window and one in the body of an iframe within that parent?

Upvotes: 0

Views: 64

Answers (2)

jfriend00
jfriend00

Reputation: 707786

An iframe is an entirely separate window and document object and a completely separate Javascript context. So, the Javascript in an iframe runs completely independently from the Javascript in its host window.

The two will have nothing to do with one another unless you specifically code them to try to interact by fetching the other one's document or window and then trying to interact with its objects or variables. If the iframe is a different domain (technically a different origin) than the host page, then the browser will even prevent most interaction between the two (security vulnerabilities). From different domains, the most the two can do is to send messages to each other (no direct interaction).


So, unless you're trying to make the two interact with purposeful coding to that aim, your two onload handlers will have nothing to do with one another.

Upvotes: 2

Marco Kerwitz
Marco Kerwitz

Reputation: 5504

The onloads won't clash in the way you may think but rather simply fire each on their own for their individual window object. Both of them would be restricted by their scope.

Upvotes: 1

Related Questions