Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
From an iframe, use the parent document's jQuery
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;

This comment has been minimized.

Show comment Hide comment

scratchoo May 7, 2016

doesn't work :(

doesn't work :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment