var $ = function(a,b){ b = b || document; return parent.jQuery.apply(this,[a,b]) };
But that is only if you are not going to use document.ready or any of its shortcuts. If you want to use any of those there is a workaround (Note: The iframe needs to have an id)
var $ = function(a,b){ var that = this; b = b || document; function ready(a){ parent.jQuery("#"+window.frameElement.id).load(function(){a.call(that)}) } if(typeof a == "function"){ ready(a) } else { var r = parent.jQuery.apply(this,[a,b]); r.ready = ready; return r } };
So now you can write jQuery like it was loaded naturally inside the iframe (using the dollar sign)
Awesome code, only problem I'm having is when trying to use it on methods like $.extend, $.trim, $.param.
ReplyDeleteAlso, not sure if it's relevant but I'm using this method between two iframes, not from parent. This iframe will never be called unless another iframe from the same domain is loaded first.
Deletefor(var x = 0; x < window.parent.frames.length; x++){
try{
if(window.parent.frames[x].location.href){
var $ = function(a,b){
b = b || document;
return window.parent.frames[x].jQuery.apply(this, [a,b])
};
break;
}
}catch(e){}
}