javascript as response text
I have an Ajax function which will retrieve some RSS feed script from server. I put this responsetext in a div using:
$("#divId").html(responsetext);
I want to execute the script inside the response. Currently, the RSS feeds not showing in the div. Is there any way to do that ? thanks..
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>
My answer from 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"
Is the response mixed HTML and JavaScript or just pure javascript?
If it's pure javascript you can always use the eval("alert('Test');")
function which executes the code sent.
精彩评论