Bug Tracker

Ticket #7436 (closed enhancement: wontfix)

Opened 4 years ago

Last modified 2 years ago

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

Reported by: tubalmartin@… Owned by:
Priority: undecided Milestone: 1.5
Component: build Version: 1.4.3
Keywords: Cc:
Blocking: Blocked by:

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!!

Change History

comment:1 follow-up: ↓ 2 Changed 4 years ago by rwaldron

  • Status changed from new to closed
  • Resolution set to wontfix
  • Component changed from unfiled to build

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

comment:2 in reply to: ↑ 1 Changed 4 years ago by anonymous

Replying to 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?

comment:3 follow-up: ↓ 4 Changed 4 years ago by 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 from the source that meets your build needs.

Version 0, edited 4 years ago by rwaldron (next)

comment:4 in reply to: ↑ 3 Changed 4 years ago by anonymous

Replying to 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.

Note: See TracTickets for help on using tickets.