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. Commented Oct 8, 2012 at 23:33
  • Here's a similar thread Commented Oct 8, 2012 at 23:34
  • Thanks krg, but I need a solution for cross domains. Commented Oct 8, 2012 at 23:49
  • 2
    Have a look at this. Haven't tried it, but it looks promising. Commented Oct 8, 2012 at 23:53
  • That's much closer to what I'm looking for. Thanks again. Commented 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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.