Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dkordik
Created February 25, 2011 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkordik/843229 to your computer and use it in GitHub Desktop.
Save dkordik/843229 to your computer and use it in GitHub Desktop.
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;
}
@scratchoo
Copy link

doesn't work :(

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