473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,402 developers and data experts.

Weather Widget with XML and Javascript

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.  
Nov 30 '07 #1
4 11357
acoder
16,027 Expert Mod 8TB
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. }
Nov 30 '07 #2
gits
5,390 Expert Mod 4TB
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
Nov 30 '07 #3
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
Dec 3 '07 #4
acoder
16,027 Expert Mod 8TB
Glad it helped.
Dec 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chris | last post by:
trying to embed a weather sticker into my web page. I am using FP 2002 with all the updates. When I enter the script all I get is text on my page. Here is the script: <script...
1
by: Douglas Crockford | last post by:
JSLint is now available as a Konfabulator widget. Drop a .js file on it, and it will tell you about its coding weaknesses. JSLint is an essential tool for quality programming in JavaScript. You...
9
by: Boki | last post by:
Hi All, I am going to create a personal weather board. It is easy to show the picutre on website ( just link the picture to picturesbox's properties ) but I dont' know how to add some other...
2
by: claan1 | last post by:
Hi, I am trying to make a widget that people can embed in thier web page,.. and I would like the widget to be able to change text values in the window it is embedded in. I dont want any javascript...
0
by: jimb | last post by:
Looking for a simple view on the weather. Just learning the xml stuff. I would like to add to the the weather.com icon. Any help is appreciated. <html> <head> <body bgcolor="#000000"...
1
by: John | last post by:
hey guys i m trying to generate a widget like reddit/delicious/google adsense widget please give pointers on how to do it thanks
3
by: dawnrager | last post by:
Hi, everyone. I'm having a really difficult time pasting in generated code from the Weather Channel for a weather sticker for my webpage. This was the code sent to me via email: <!-- cut and...
2
by: JayCally | last post by:
I want to add the current weather conditions in my area to my site. I've looked at some of the sites that will give you a generated script to add to your site but I don't like the way they look. I...
2
by: Yi | last post by:
I want to make a simple javascript widget, something looks like the Google AdWords, that people can just post a small section of code on their web page and display some content from my website. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.