Connecting Tech Pros Worldwide Help | Site Map

Weather Widget with XML and Javascript

Newbie
 
Join Date: Nov 2007
Location: Tasmania, Australia
Posts: 2
#1   Nov 30 '07
I wrote this script to calculate if water restrictions were in place if it was hotter than 25deg C. It has turned out to be a pretty good weather widget that can be used elsewhere. This was initially written for Northern Midlands Council in Tasmania, Australia. I wrote this for Internet explorer and haven't tested it elsewhere. It would probably work better if it used XMLHttpRequest Object, I think I will try this next time. Let me know what you think!

Cheers!

Shayne

Expand|Select|Wrap|Line Numbers
  1. /* 
  2. Shaynes Weather Script
  3. --------------------------
  4. -Shayne Nash 21/11/07
  5. -Northern Midlands Council
  6. ---------------------------
  7.  
  8. What it does:
  9. -Displays weather information one piece at a 
  10.  time from XML Data source
  11.  
  12. How:
  13. -Loads data from an XML and places data in array
  14. -Cycles through data in array
  15. -Removes bad strings
  16. -Writes the data to span element with id "items"
  17.  
  18. Programming info:
  19. -Variables prefixed with "_" are globals.
  20. -place/link script in head.
  21. -place loadXML() in Body onload attribute to load 
  22.  the external data in, self.setInterval("animate()") takes care of 
  23.  the rest.
  24. -Does not like to be used across internet, cache 
  25.  XML on own domain or enable "Access datasources across 
  26.  domains" for the zone the script will be accessed in (at own risk).
  27.  -info array elements are strings
  28.  -Permission is given to use, copy, modify, distribute but
  29.   at your own risk and no warranty or support implied.
  30.  -temp.jpg and speed.gif are not my images so do not use on 
  31.   a live website.
  32.  
  33. ----------------------------------------------------------*/
  34.  
  35. //Keep writing info to the span every 2.5 seconds
  36. var int = self.setInterval("animate()",2500);
  37.  
  38. //Globals
  39. var _xmlDoc; //XML buffer
  40. var _info = new Array(); //Array of information from XML
  41. var _i = 0;//Array index tracking
  42.  
  43. //filter out the naughty strings
  44. function filterAlpha(data)
  45. {    
  46.     if (data.match(/^[-]?\d*\.?\d*$/))
  47.     {
  48.         //Data is good output the data
  49.         return (data);
  50.     }
  51.     //data is bad, don't output the data    
  52.     return ("0");//this string can be changed to a friendly error message
  53. }
  54.  
  55. //Load the xml and place the data in the info array
  56. function loadXML()
  57. {
  58. // ActiveXobject only works with Internet Explorer
  59.     if (window.ActiveXObject)
  60.     {      
  61.           //Create the XML object
  62.           _xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  63.           _xmlDoc.async=false;
  64.           //Grab the XML from here
  65.           _xmlDoc.load("weather.xml");
  66.           //Fill the Array 
  67.           _info[0] = filterAlpha(_xmlDoc.getElementsByTagName("w:forecast")[0].attributes[3].value);
  68.                    //filterAlpha, Just in case the xml owner/hacker puts javascript in the data    
  69.           _info[1] = filterAlpha(_xmlDoc.getElementsByTagName("w:current")[0].attributes[0].value);      
  70.           _info[3] = filterAlpha(_xmlDoc.getElementsByTagName("w:current")[0].attributes[2].value);
  71.           _info[4] = filterAlpha(_xmlDoc.getElementsByTagName("w:current")[0].attributes[3].value);    
  72.           _info[2] = "<img src='water.gif' height=50px>Water Restrictions: None";
  73.           if (parseInt(_info[0]) >= 25)
  74.           {
  75.               _info[2] = "<img src='water.gif' height=50px>Water Restrictions: Apply"; 
  76.           }
  77.           _info[0] = "<img src='temp.jpg' height=50px>Forecast Temp: " + _info[0] + " °C";
  78.           _info[1] = "<img src='temp.jpg' height=50px>Current Temp: " + _info[1] + " °C";
  79.           _info[3] = "<img src='humidity.gif' height=50px>Humidity: " + _info[3] + "%";
  80.           _info[4] = "<img src='speed.gif' height=50px>Wind Speed: " + _info[4] + " km/h";
  81.     }
  82. }
  83.  
  84. //Change the Inner HTML of the Span
  85. //and index the next element if exists
  86. function animate()
  87. {
  88.     //Change Span "Items"
  89.     document.getElementById("items").innerHTML = _info[_i];
  90.     _i++; //Move to next array index
  91.     //if the index doesnt exist then start again
  92.     if (_i >= _info.length) 
  93.     {
  94.         _i = 0;
  95.     }    
  96. }
  97.  
  98. //End of Script
  99.  



acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2   Nov 30 '07

re: Weather Widget with XML and Javascript


That would only work in IE. For a cross-browser solution, add the following:
Expand|Select|Wrap|Line Numbers
  1. else if (document.implementation && document.implementation.createDocument) {
  2.   _xmlDoc=document.implementation.createDocument("","",null);
  3.   _xmlDoc.load("weather.xml");
  4.   _xmlDoc.onload = somefunction;
  5. }
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,127
#3   Nov 30 '07

re: Weather Widget with XML and Javascript


hi ...

a small optimization to mention - you should use a reference like:

Expand|Select|Wrap|Line Numbers
  1. var w_curr = _xmlDoc.getElementsByTagName("w:current")[0];
since you wrote it 3 times in the script. it's not only to shorten the lines ... but to optimize the runtime because js didn't need to parse the dom 3 times any longer ...

kind regards
Newbie
 
Join Date: Nov 2007
Location: Tasmania, Australia
Posts: 2
#4   Dec 3 '07

re: Weather Widget with XML and Javascript


Thank you both very much! Both of these suggestions worked excellent. I am very new to DOM and I am very grateful for your valuable knowledge.

Cheers!

Shayne
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5   Dec 5 '07

re: Weather Widget with XML and Javascript


Glad it helped.
Reply