2012-10-08 52 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>'); 

我還想寫一些可執行的JavaScript代碼在這個子文件與被寫的iFrame內容合作到父文件。這可能嗎?我試過這個:

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

我在做什麼錯?

+0

幾點建議:儘量遠離'document.write'。不要使用全局變量,創建自己的名稱空間(對象)。通過這種方式瞭解AJAX,您將不需要那麼大的src行,並且您的代碼將變得更加明顯易於維護。 – elclanrs

+0

這裏有一個類似的[線程](http://stackoverflow.com/questions/5477324/iframe-calling-parent-javascript) – krg

+0

謝謝krg,但我需要一個跨域的解決方案。 –

回答

相關問題