Last active

From an iframe, use the parent document's jQuery

  • Download Gist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Lets hope you never have to do this, but if you're in an iframe that needs to use jQuery,
and you don't want to load the file a separate time in the iframe, this will let you use
the parent page's copy of jQuery, while also giving you the correct scope for selectors
to work as expected.
This version is SOL if you want to still be able to set a sub-scope from the iframe.
if (typeof(jQuery) == "undefined") {
var iframeBody = document.getElementsByTagName("body")[0];
var jQuery = function (selector) { return parent.jQuery(selector, iframeBody); };
var $ = jQuery;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.