ajax中执行服务器返回的js脚本

前端之家收集整理的这篇文章主要介绍了ajax中执行服务器返回的js脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在ajax的回调函数中,先获取js脚本的值,赋值到页面的某一个控件,比如一个div,如:

document.getElementById("uccenter").innerHTML=data.ucString; //赋值给id为uccenter的div

然后获取此div中的script对象,如:

var myDivScript = document.getElementById("uccenter").getElementsByTagName("script").item(0);

再创建一个新的script对象,因为只有新的对象页面才会执行

var newScript = document.createElement("script");

把js脚本的属性值赋值给新的script对象

 newScript.setAttribute("src",myDivScript.getAttribute("src"));
				 newScript.setAttribute("reload",myDivScript.getAttribute("reload"));
				 newScript.innerHTML = myDivScript.innerHTML;
最后,添加心script对象到页面
document.getElementsByTagName("HEAD").item(0).appendChild(newScript);

// 用户注册完成回调函数001
	function loginSuccessAjaxCallback(data){
		if(null == data.resultString || "" == data.resultString)
		{
			if(null != data.ucString || "" != data.ucString)
			{
                 document.getElementById("uccenter").innerHTML=data.ucString; 
				 var myDivScript = document.getElementById("uccenter").getElementsByTagName("script").item(0);
				 var myDivScript2 = document.getElementById("uccenter").getElementsByTagName("script").item(1);
				 var newScript = document.createElement("script");
				 var newScript2 = document.createElement("script");
				 newScript.setAttribute("src",myDivScript.getAttribute("reload"));
				 newScript.innerHTML = myDivScript.innerHTML;
				 
				 newScript2.setAttribute("src",myDivScript2.getAttribute("src"));
				 newScript2.setAttribute("reload",myDivScript2.getAttribute("reload"));
				 newScript2.innerHTML = myDivScript2.innerHTML;
				 document.getElementsByTagName("HEAD").item(0).appendChild(newScript); 
				 document.getElementsByTagName("HEAD").item(0).appendChild(newScript2); 
			}
			window.location.reload();
		}
		else
		{
			$('#loginResuleMessage').html(data.resultString);
		}
	}

这里的js脚本形如:


<script type="text/javascript" src="http://localhost/bbs/api/uc.PHP?time=1388937248&code=ae28TF01utAnYLk0%2BhVarCC0mO8hxTN7K7JoPAHmkHN3sX1OQBv6IExLuUA66n78X%2B8mSnvDpAThuBSOm3ph9NqKHJbn3gMQ5W%2Bmyn%2BbFwYQ8zMaIV5BXhpYzoR0Hx0a6Qa02e0gcNH%2B2ROVk8MF4Ms8CUFeyBMVuBahxbCeaQ" reload="1"></script><script type="text/javascript" src="http://localhost:8080/yxw/api/uc.PHP?time=1388937248&code=1752B0%2FcsBy%2FZkYV0Hf2cxoxk5tx2IK4CwhYqVRbvuzcqL051vFGWmq5E0vaJw7tIXqDZb%2BEAtgPPNkgten9owDk%2FXu%2B5YRJ8XBT6tjlHery2BtZPNjlUyZKBtamHs6%2FLEUPTciQqB5vuxjW0%2F1ib%2FTqpZL8CweG%2FPkgFiZ41Q" reload="1"></script>
原文链接:https://www.f2er.com/ajax/165356.html

猜你在找的Ajax相关文章