/ Published in: JavaScript
Simple Javascript that will get a feed and parse it using Ajax
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function SendAjax(url, callbackFunction) { if (window.XMLHttpRequest) { var request = new XMLHttpRequest(); } else { var request = new ActiveXObject("Microsoft.XMLHTTP"); } request.open("POST", url, true); request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.onreadystatechange = function() { if (request.readyState == 4 && request.status == 200) { if (request.responseText) { ReceiveAjax(request.responseText, callbackFunction); } } } request.send(null); } function ReceiveAjax(response, callbackFunction) { if (window.ActiveXObject) { var doc = new ActiveXObject("Microsoft.XMLDOM"); doc.async = "false"; doc.loadXML(response); } else { var parser = new DOMParser(); var doc = parser.parseFromString(response,"text/xml"); } callbackFunction(doc.documentElement); } function ValueFromTagName(item, tagname) { var val = item.getElementsByTagName(tagname); return val[0].firstChild.nodeValue; } function ParseData(data) { var items = data.getElementsByTagName('item'); var output = '<ul>'; for (var i = 0; i < items.length; ++i) { var title = ValueFromTagName(items[i], 'title'); var link = ValueFromTagName(items[i], 'link'); output += '<li><strong><a href ="' + link + '">' + title + '</strong> ' + '</li>'; } output += '</ul>'; var RSSOutput = document.getElementById('RSSOutput'); RSSOutput.innerHTML = output; } function LoadFeed( url ) { var GetFeed = url; SendAjax('getfeed.php?url=' + escape(GetFeed.value), ParseData); } LoadFeed( "http://del.icio.us/rss/twelvetone" );