Skip to main content

Bug Tracker

Side navigation

#7436 closed enhancement (wontfix)

Opened November 08, 2010 04:44PM UTC

Closed November 08, 2010 05:20PM UTC

Last modified March 13, 2012 07:22PM UTC

jqueryContext: Enable jQuery functions in iframes without loading jQuery in them.

Reported by: tubalmartin@gmail.com Owned by:
Priority: undecided Milestone: 1.5
Component: build Version: 1.4.3
Keywords: Cc:
Blocked by: Blocking:
Description

On a recent project I'm working on (not published yet, sorry) I use several iframes.

I needed the jQuery library to be available in the main window as well as in every iframe.

Currently (1.4.x), jQuery autoruns sandboxed, so referencing jQuery from inside an iframe is useless:

// iframe context
var $ =  window.top.$; 
/* reference to the $ function in parent window does work, but it's useless 
because selector context is defined exclusively for the window that required 
jQuery and jQuery does not provide any method to change the selector context.*/

So, the only way to have jQuery support in every iframe is to require jQuery in every iframe which in my opinion is a sub-optimal solution.

In my case I was dealing with more than 4 iframes and didn't like the idea of including jQuery in all of them so I modified jQuery's initialization:

// I simply changed jQuery's 1.4.3 first and last lines from this:

(function( window, undefined ) {
// ........
// ........
})(window);

// to this:

window.jQueryContext = function(window, undefined){
// ......
// ......
};
window.jQueryContext(window);

This way, I just need to require jQuery in the main window and in my iframes I can call jQueryContext to initialize jQuery and specify the correct window context while preserving all of jQuery's integrity.

// Note: iframe context. jQuery not loaded in iframe, only in top window

// Initialize jQuery and set jQuery's selector context to iframe window

window.top.jQueryContext(window);

// Make magic with jQuery in iframe's DOM

$(....);

Think about it, it's just one more global variable: $, jQuery & jQueryContext.

If you find a better way of solving this "issue", i'll be glad to see it implemented of course, my solution is just a suggestion ;)

Many people scratch their heads over the net asking for this kind of solution, simply google it.

Thanks in advance, jQuery is awesome!!

Attachments (0)
Change History (4)

Changed November 08, 2010 05:20PM UTC by rwaldron comment:1

component: unfiledbuild
resolution: → wontfix
status: newclosed

I recommend writing a plugin, writing docs for your plugin's api, writing demos, writing unit tests and publishing this all to github.

Changed November 08, 2010 05:31PM UTC by anonymous comment:2

Replying to [comment:1 rwaldron]:

I recommend writing a plugin, writing docs for your plugin's api, writing demos, writing unit tests and publishing this all to github.

Emm...a plugin?

It would be impossible to achieve what I explain with a plugin, it makes nonsense.

Have you read my ticket at all?

Changed November 08, 2010 07:43PM UTC by rwaldron comment:3

_comment0: I've consulted with the rest of the bug triage team and we've agreed that at the current time this is simply not a case that jQuery intends to support. \ \ But to make up for being short with you, I've come up with a solution that might work well for your needs: https://gist.github.com/668152 \ \ This is a substitute for jQuery's bundled makefile that will create a version of jquery from the source that meets your build needs.1289245457997817

I've consulted with the rest of the bug triage team and we've agreed that at the current time this is simply not a case that jQuery intends to support.

But to make up for being short with you, I've come up with a solution that might work well for your needs: https://gist.github.com/668152

This is a substitute for jQuery's bundled makefile that will create a version of jquery that is built to your spec.

Changed November 08, 2010 07:56PM UTC by anonymous comment:4

Replying to [comment:3 rwaldron]:

I've consulted with the rest of the bug triage team and we've agreed that at the current time this is simply not a case that jQuery intends to support. But to make up for being short with you, I've come up with a solution that might work well for your needs: https://gist.github.com/668152 This is a substitute for jQuery's bundled makefile that will create a version of jquery that is built to your spec.

Thanks Rick for your response and the make file, appreciated.