网站建议:179001057@qq.com

ajax接收服务端并执行js代码

技术2022-06-23  7

client.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Test Ajax </title> <mce:script type="text/javascript"><!-- var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } } function TestGo(){ createXMLHttpRequest(); xmlHttp.onreadystatechange = handleTest; var url = "Server.php"; xmlHttp.open("GET", url, true); xmlHttp.send(null); } function handleTest(){ if(xmlHttp.readyState == 4 && xmlHttp.status == 200){ var data = xmlHttp.responseText; /*开始处理是否有js代码*/ var reg_onload = /<body onLoad="([^<]*)">/; var match_onload = data.match(reg_onload); var reg = /<script[^>]*>([/s/S]*)<//script>/; var match_data = data.match(reg); var MyScript = ""; //alert(match_data); if(match_data != null){ MyScript = match_data[1]; var script = document.createElement("script"); script.text = MyScript; document.getElementsByTagName("head")[0].appendChild(script); } var Html = data.replace(reg, ""); var container = document.getElementById("show"); container.innerHTML = Html; if(match_onload != null){ eval(match_onload[1]); } } } // --></mce:script> </head> <body> <input type="button" value="TestGo" οnclick="TestGo();" /> <div id="show"></div> </body> </html>

 

server.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Ajax Server</title> <mce:script type="text/javascript"><!-- function testServer(){ alert("What are you doing?"); } // --></mce:script> </head> <body> <div id="divServer" style="border: solid 2px #F00;" mce_style="border: solid 2px #F00;"> <input type="button" value="Server" οnclick="testServer();" /> </div> </body> </html>


最新回复(0)