不管別人怎麼說,不要用EVAL。這是邪惡的,如果在頁面上使用超過幾次,它會給你記憶力問題。
基本上,創建 「codeHolder」 瞧的ID一個div。您基本上想要將您的HTML和JS傳回給AJAX接收器(由分隔符分隔),在JS端解析它,顯示HTML並將JS代碼放入您的變量中。
//Somehow, get your HTML Code and JS Code into strings
var javascriptCode="function test(){.....}";
var htmlCode="....";
//HTML /
//Locate our HTML holder Div
var wndw=document.getElementById("display");
//Update visible HTML
wndw.innerHTML = htmlCode;
//Javascript ///
//Create a JSON Object to hold the new JS Code
var JSONCode=document.createElement("script");
JSONCode.setAttribute("type","text/javascript");
//Feed the JS Code string to the JSON Object
JSONCode.text=javascriptCode;
//Locate our code holder Div
var cell=document.getElementById("codeHolder");
//Remove all previous JS Code
if (cell.hasChildNodes())
while (cell.childNodes.length >= 1)
cell.removeChild(cell.firstChild);
//Add our new JS Code
cell.appendChild(JSONCode);
//Test Call///
test();
該代碼將替換您可能在那裏放置的所有以前的JS代碼與新的JS代碼字符串。