Coffee Bite Coffee Bite - 1 month ago 7
Javascript Question

How can I inject a Jquery object from a parent iframe to an embedded iframe?

I am embedding an iframe in a webpage and I want to inject jquery in this frame.

I tried something like:

frame.contentWindow.$ = window.$


While this works, the iframe's $ is still running in the parent's scope as opposed to its own independent copy.

Is there a way to inject an independent jquery object into the iframe without injecting a
<script src="/path/to/jquery.js"></script>
in the frame?

UPDATE

Solved it. The jquery.js conveniently defines jQuery for a scope by accepting the window object in an anonymous function:

function(windowObject){ .... define jquery }(window)


So all I had to do was to name this function, E.g. jQuerify and call it once for the parent window object and then for the child window object.

jQuerify = function(windowObject){ .... define jquery }
jQuerify(window)
jQuerify(iframe.contentWindow)

Answer

Solved it. The jquery.js conveniently defines jQuery for a scope by accepting the window object in an anonymous function:

function(windowObject){ .... define jquery }(window) So all I had to do was to name this function, E.g. jQuerify and call it once for the parent window object and then for the child window object.

jQuerify = function(windowObject){ .... define jquery }
jQuerify(window)
jQuerify(iframe.contentWindow)