如何在使用jquery ajax或post插入DOM时停止评估脚本标记

时间:2010-03-04 22:04:37

标签: jquery ajax scripting

如何停止评估jquery ajax中的脚本标签并发布响应? 请在下面找到相应的代码。即使我使用dataType : 'text'

,也会评估函数sendForm()
<HTML>
 <HEAD>
 <TITLE>This is the title</TITLE> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script> 
<SCRIPT> 
function get_info(url,data,callback,errorFunction)
{  
 $.ajax({ type: 'POST',   url: url,   data: data,   success: callback,   dataType: 'text',   error: errorFunction,   cache : false });
} 
</SCRIPT> 
</HEAD>
<BODY>
<A NAME="this"></A> <A HREF="#this" ONCLICK="get_info('Result1.lasso','temp=1&testParam=Rajesh',function(response){$(document.getElementById('test')).html(response);},function(){$(document.getElementById('test')).html('Error');})">Some Text</A> 
<DIV id = 'test'></DIV>
</BODY>
</HTML>

Result1.Lasso

<script type="text/javascript">
    function sendForm()
    {   
        alert('hi');
    }
</script>
<form name= "abc" method = "get" action = "abcd.lasso">
    <input type ="text" name = "element1"/>
    <input type = "button" value="Click" onClick = "javascript: sendForm();"/>
</form>

1 个答案:

答案 0 :(得分:1)

我能说出正确执行脚本的原因是因为这行代码:

$(document.getElementById('test')).html(response)

由于您是以html格式添加它,因此您要添加所有html元素,包括脚本。如果你将html()更改为text(),你将不会执行javascript,但我认为它看起来不像你想要它如何工作。

我最好的建议是在添加之前使用javascript字符串函数从响应字符串中提取任何javascript。如果您可以控制.lasso,这是一个很好的解决方案。如果你不这样做,如果你的javascript搜索不好,可能会出现一些安全问题。