2012-10-08 3 views
1

두 개의 JavaScript 파일이 있습니다. 첫 번째 파일은 변수를 설정하고 다른 파일의 내용이 iframe.js라고 기록 :상위 JavaScript 파일에 대해 실행 가능한 jQuery를 작성하려면 어떻게해야합니까?

/* 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>'); 
내가 기록 된 IFrame 콘텐츠 : 작업을이 아이 파일에 일부 실행 자바 스크립트 코드를 작성하고자하는

상위 파일로 이것이 가능한가? 나는 이것을 시도했다 :

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

내가 뭘 잘못하고 있니?

+0

몇 가지 조언 : 가능한 경우 언제든지 'document.write'에서 벗어나십시오. 전역 변수를 사용하지 말고 고유 한 네임 스페이스 (객체)를 만듭니다. 커다란 src 라인이 필요없고 코드가 더 쉽게 유지 될 수 있도록 AJAX에 대해 배웁니다. – elclanrs

+0

여기에 비슷한 [스레드] (http://stackoverflow.com/questions/5477324/iframe-calling-parent-javascript) – krg

+0

감사 krg,하지만 크로스 도메인에 대한 솔루션이 필요합니다. –

답변