javascript作为响应文本

时间:2010-06-16 09:41:59

标签: javascript jquery ajax

我有一个Ajax函数,它将从服务器检索一些RSS feed脚本。我使用以下命令将此responsetext放在div中:

$("#divId").html(responsetext);

我想在响应中执行脚本。目前,RSS提要未显示在div中。有没有办法做到这一点?感谢..

responsetext
-------------  -

<div class="last-updated"  style="display: block" id="p-lastUpdated">Last Updated:16 Jun 2010 10:32 AM</div> 

<div id="digg-widget-1276598296115">

<a href="http://digg.com/search?s=timesofindia.indiatimes.com">See more timesofindia.indiatimes.com stories.</a>

</div>

<script type="text/javascript">

(function() { var s, s1, diggWidget = {id: "digg-widget-1276598296115", layout: 1, colors: {hdrBg: "#1b5790", hdrTxt: "#b3daff", tabBg: "#4684be", tabTxt: "#b3daff", tabOnTxt: "#d41717", bdyBg: "#fff", stryBrdr: "#ddd", lnk: "#105cb6", descTxt: "#999999", subHd: "#999999"}, title: "RSS Times of india", width: 480,height: 300, requests: [{t: "timesofindia.indiatimes.com", p: {count: "10", sort: "promote_date-desc", method: "story.getPopular", domain: "timesofindia.indiatimes.com"}}], hide: {footer: true, header: true}, target: "_blank", descriptions: "show", rounded: true}; if (window.DiggWidget) { if (typeof DiggWidget == 'function') { new DiggWidget(diggWidget); } else { DiggWidget.push(diggWidget); } } else { DiggWidget = [diggWidget]; s = document.createElement('SCRIPT'); s.type = 'text/javascript'; s.async = true; s.src = 'http://widgets.digg.com/widgets.js'; s1 = document.getElementsByTagName('SCRIPT')[0]; s1.parentNode.insertBefore(s, s1); } })();

</script>

2 个答案:

答案 0 :(得分:0)

响应是混合HTML和JavaScript还是纯粹的javascript?

如果它是纯JavaScript,您可以随时使用执行发送代码的eval("alert('Test');")函数。

答案 1 :(得分:0)

我的答案来自How To Call Javascript In Ajax Response? IE: Close a form div upon success

// response is the data returned from the server
var response = "html\<script type=\"text/javascript\">alert(\"foo\");<\/script>html";
var reScript = /\<script.*?>(.*)<\/script>/mg;
response = response.replace(reScript, function(m,m1) {
    eval(m1); //will run alert("foo");
    return "";
});
alert(response); // will alert "htmlhtml"