Home arrow JavaScript arrow Page 4 - Reading Data from an XML File for a Dynamic AJAX-based Banner System
JAVASCRIPT

Reading Data from an XML File for a Dynamic AJAX-based Banner System


Undeniably, AJAX has introduced a noticeable impact into the development of rich web applications, and consequently there are many enthusiastic web developers that continue discovering more and more creative uses for this powerful technology. Particularly, in this series of articles this "creativity" is focused entirely upon building an AJAX-driven banner application for displaying, on the browser, a certain number of banners via a few HTTP requests made in the background.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
July 31, 2007
TABLE OF CONTENTS:
  1. · Reading Data from an XML File for a Dynamic AJAX-based Banner System
  2. · Reintroducing the source files of the initial banner application
  3. · Fetching banner data from an XML file
  4. · Completing the improved AJAX-based banner application

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Reading Data from an XML File for a Dynamic AJAX-based Banner System - Completing the improved AJAX-based banner application
(Page 4 of 4 )

As I said in the previous section, the source code for the updated AJAX application is listed below. It is comprised of only two source files (the prior one required one more), whose respective signatures are listed below:

(definition of 'dynamic_banner.htm' file)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>AJAX-Driven Dynamic Banner System</title>
<script language="javascript" type="text/javascript">
// send http requests
function sendHttpRequest(url,callbackFunc,respXml){
  
var xmlobj=null;
  
try{
    
xmlobj=new XMLHttpRequest();
  
}
  
catch(e){
    
try{
      
xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
    
}
    
catch(e){
      
alert('AJAX is not supported by your browser!');
      
return false;
    
}
  
}
  
xmlobj.onreadystatechange=function(){
    
if(xmlobj.readyState==4){
      
if(xmlobj.status==200){
        
respXml?eval(callbackFunc+'(xmlobj.responseXML)'):eval
(callbackFunc+'(xmlobj.responseText)');
      
}
    
}
  
}
  
// open socket connection
  
xmlobj.open('GET',url,true);
  
// send http header
  
xmlobj.setRequestHeader('Content-Type','text/plain;
charset=UTF-8');
  
// send http request
  
xmlobj.send(null);
}
// store banners into JavaScript array
function storeBanners(banners){
  
var banners=banners.getElementsByTagName('banner');
  
if(!banners){return};
  
// save banner data to JS arrays
  
for(var i=0;i<banners.length;i++){
    
images[i]=banners[i].getElementsByTagName('image')
[0].firstChild.nodeValue;
    
urls[i]=banners[i].getElementsByTagName('url')
[0].firstChild.nodeValue;
  
}
  
// display first banner
  
displayBanner();
}
// display banners
function displayBanner(){
  
var bannercont=document.getElementById('bannercontainer');
  
if(!bannercont){return};
  
// clean up headlines container
  
bannercont.innerHTML='';
  
// create <img> element
  
var img=document.createElement('img');
  
img.setAttribute('src',images[index]);
  
// create <a> element
  
var a=document.createElement('a');
  
a.setAttribute('href',urls[index]);
  
img.setAttribute('width',180);
  
img.setAttribute('height',400);
  
a.appendChild(img);
  
// append banner to banner container
  
bannercont.appendChild(a);
  
index++;
  
if(index>urls.length-1){index=0};
  
setTimeout("displayBanner()",15*1000);
}
// initialize data arrays and index
var images=new Array();
var urls=new Array();
var index=0;
window.onload=function(){
  
if(document.getElementById&&document.
getElementsByTagName&&document.createElement){
    
// fetch the first banner after loading the web page
    
sendHttpRequest('banners.xml','storeBanners',true);
  
}
}
</script>
<style type="text/css">
body{
  
margin: 0;
  
padding: 0;
  
background: #eee;
}

h1{
  
text-align: center;
  
font: bold 24px Arial, Helvetica, sans-serif;
  
color: #000;
}

#bannercontainer{
  
text-align: center;
  
width: 180px;
  
height: 400px;
  
margin-left: auto;
  
margin-right: auto;
  
background: #fff;
  
border: 1px solid #000;
}

#bannercontainer img{
  
border: none;
}
</style>
</head>
<body>
  
<h1>AJAX-Driven Dynamic Banner System</h1>
 
<div id="bannercontainer"></div>
</body>
</html>

(definition of 'banners.xml' file)

<?xml version="1.0" encoding="iso-8859-1"?>
<banners>
 
<banner>
   
<image>banner1.gif</image>
   
<url>http://www.myhosting.com</url>
 
</banner>
 
<banner>
   
<image>banner2.gif</image>
   
<url>http://www.myphp.com</url>
 
</banner>
 
<banner>
   
<image>banner3.gif</image>
   
<url>http://www.myjs.com</url>
 
</banner>
</banners>

And wrapping up, the full source code corresponding to this application wouldn't be complete if I don't show you once again the sample banner images that I created in the previous article of the series, so here they are:

Final thoughts

In this second installment of the series, I introduced a major improvement into the original AJAX-based banner application by modifying one of its principal JavaScript functions. As you saw, the direct consequence in doing this was the relocation of the corresponding banner data into a new compact XML file, and the elimination of a PHP file that reads this data from the web server.

Nonetheless, the application still relies on a global JavaScript variable to control the sequence in which the banners are displayed on the browser. This can be considered a bad programming habit. Thus, in the last part of the series, I'm going to fix this issue, in addition to tweaking the source code a bit, so all the banner data can be fetched from MySQL.

Now that you've been warned, are you going to miss it? I hope not!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials