Search code examples
javascriptjqueryiframeiframe-app

How do I write executable jQuery for a parent JavaScript file?


I've got two JavaScript files. This first file sets up variables and writes the contents of another file called iframe.js:

/* Parent File  */
var aff_id = 2; // Your affiliate ID
var aff_source_id='1'; // Your site ID
var aff_source_subid=''; // Your custom tracking ID
var thankyouUrl = ''; // Leave blank if you don't have a thank you page
var live = 0; // 1 for production
var lead_type = 1; // Auto insurance
document.write(unescape('%3Cscript src="'+site+'/api/js/iframe.js"%3E%3C/script%3E'));
/* Child File */
if(!site) var site='https://leadthis.com';
$(function(){
    var height,width,src = 'http://lead/api/iframe/insurance/auto.php#'+encodeURIComponent(document.location.href )+'?aff_id='+aff_id+'&aff_source_id='+encodeURIComponent(aff_source_id)+'&aff_source_subid='+encodeURIComponent(aff_source_subid)+'&live='+live,
    emarfi=$('<iframe id="appframe" src="'+src+'" scrolling="no" frameborder="0"><\/iframe>').appendTo('#emarfi');
});
document.write('<div id="emarfi"><\/div>');

I would like to write some executable JavaScript code in this child file to work with the iframed content that is written to the parent file. Is this possible? I tried this:

document.write("$(function(){ $('iframe').load(function(){alert('Hello Iframe');})});");

What am I doing wrong?


Solution

  • I've started using Porthole based off krg's comment, and that is exactly what I need. Thanks. http://ternarylabs.com/2011/03/27/secure-cross-domain-iframe-communication/