2010-03-04 15 views
2

如何停止評估jquery ajax中的腳本標記併發布響應? 請找到相應的代碼below.The功能sendForm(),即使我使用dataType : 'text'如何停止使用jquery ajax插入到DOM中時評估腳本標記或發佈

<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

最好的,我可以告訴腳本是作爲原因正在評估執行是因爲該行的代碼:

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

既然你是在將其添加爲HTML你添加所有的HTML元素,包括腳本。如果將html()更改爲text(),則不會執行javascript,但我不認爲它會看起來如何讓它工作。

我最好的建議是在添加javascript字符串函數之前將任何javascript從響應字符串中提取出來。如果您可以控制.lasso,這是一個好的解決方案。如果你的javascript搜索不好,你可能不會發生一些安全問題。