2010-04-17 56 views
2

我期待一個腳本添加到一個iFrame的頭球,而不是失去包含在iframe的身體或頭部的一切......插入腳本到一個iframe的頭,但不清除出身體的iFrame

這裏是我現在正在使用新腳本更新iFrame,但它清除了iframe中的所有內容,而不是追加哪些是我想要的內容。 thxs! B

// Find the iFrame 
    var iframe = document.getElementById('hi-world'); 

    // create a string to use as a new document object 
    var val = '<scr' + 'ipt type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></scr' + 'ipt>'; 

    // get a handle on the <iframe>d document (in a cross-browser way) 
    var doc = iframe.contentWindow || iframe.contentDocument; 
    if (doc.document) { doc = doc.document;} 

    // open, write content to, and close the document 
    doc.open(); 
    doc.write(val); 
    doc.close(); 

回答

0
var headID = document.getElementsByTagName("head")[0];   

var newScript = document.createElement('script'); 
newScript.type = 'text/javascript'; 
newScript.src = 'yourpath'; 
headID.appendChild(newScript);