1

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?

5
  • An few advices: stay away from document.write whenever you can. Don't use global variables, create your own namespace (object). Learn about AJAX that way you won't need that huge src line and your code will become more obvious an easier to maintain.
    – elclanrs
    Oct 8, 2012 at 23:33
  • Here's a similar thread
    – krg
    Oct 8, 2012 at 23:34
  • Thanks krg, but I need a solution for cross domains.
    – Eric Leroy
    Oct 8, 2012 at 23:49
  • 2
    Have a look at this. Haven't tried it, but it looks promising.
    – krg
    Oct 8, 2012 at 23:53
  • That's much closer to what I'm looking for. Thanks again.
    – Eric Leroy
    Oct 8, 2012 at 23:55

1 Answer 1

2

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.