Sharing javascript between iframes

Mike Rumble
Mon, 24 Jul 2006 12:39:12 -0000

Hi,

I''m looking for a way to share prototype.js, and some of the
scriptaculous stuff between multiple iframes on a single page, loading
the javascript files once only, and in the parent page for this iframes.

Conventional wisdom might see me doing something like this from within
the iframes to access the prototype functions...

var element = parent.$(''element_id'');

The problem of course with this, and this isn''t limited to prototype,
is
that the document keyword refers to the parent not the iframe, so for
example, in the $ function, document.getElementById will look for an
element with the id of ''element_id'' within the parent
document, not the
iframe.

One variation i''ve thought of is to pass in a reference to the iframe
document to the function, so the $ function would look something like
this...

var element  = parent.$(''element_id'', iframe);

The problem with this is that then means going through and changing
every document keyword to a variable throughout prototype and would end
up being pretty messy.

So, has anyone had any experience working with this kind of problem, or
any ideas how to go about implementing this, and if so would you be kind
enough to share your knowledge here?

The alternative would be to load prototype in the document content of
the iframe, thus meaning multiple instances of prototype would be in
existence. Is this such a bad thing, would it lead to a performance hit?

Thanks in advance for any help.

Cheers,
Mike.