Search This Blog

Tuesday, August 17, 2010

Ajax Feed

<base href="http://www.ajaxdaddy.com/ajax/ajax-feed/">
<style type="text/css">
body {color: white;background: #52616F;}
a { color: white; }
</style>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>XMLHttpRequest() demo</title>


<style type="text/css">
body {
background-color: #52616F;
color: white;
}
a {
color: pink;
}
#feed {
background: #52616F;
border: 1px dotted #ccc;
height: 400px;
overflow: auto;
width: 400px
}
</style></head><body>

<script language="javascript" type="text/javascript">

function showFeed(xmlUrl, xslUrl) {
var feed = document.getElementById('feed');
//clear feed div
while(feed.hasChildNodes()){
feed.removeChild(feed.childNodes[0]);
}
//append new htmlfragment
feed.appendChild(getHtmlFragment(xmlUrl, xslUrl));
}

function getHtmlFragment(xmlUrl, xslUrl) {
var xslStylesheet;
var xsltProcessor = new XSLTProcessor();

//load the xml file
var xmlSource = getResponseXml(xmlUrl).responseXML;

//load the xsl file into the xslt Processor
xslStylesheet = getResponseXml(xslUrl).responseXML;
xsltProcessor.importStylesheet(xslStylesheet);

return xsltProcessor.transformToFragment(xmlSource, document);
}

function getResponseXml(xmlUrl) {
var xmlHttp = ajaxFunction();
xmlHttp.open("GET", xmlUrl, false);
xmlHttp.send(null);
return xmlHttp;
}

function ajaxFunction()
{
var xmlHttp;

try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
return xmlHttp;
}
</script>
<p>Retrieves an xml rss feed and xsl stylesheet with XMLHttpRequest().
Applies the stylesheet to the xml. Adds the fragment to the div.</p>

<a href="#" onclick="showFeed('rss.xml', 'rsscontents.xsl'); return false;">table of contents</a> -
<a href="#" onclick="showFeed('rss.xml', 'rsscomplete.xsl'); return false;">complete entries</a>

<center><div id="feed"></div></center>

</body>

No comments:

Post a Comment