473,546 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weather Widget with XML and Javascript

2 New Member
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 11374
acoder
16,027 Recognized Expert Moderator MVP
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 Recognized Expert Moderator Expert
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
shaynenash
2 New Member
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 Recognized Expert Moderator MVP
Glad it helped.
Dec 5 '07 #5

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

Similar topics

1
8990
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 src='http://voap.weather.com/weather/oap/80249?template=TRVLV&par=1003195453 &unit=0&key=8ab1009c5e741b96875cf89fb5e21ee2'></script>; It is a weather sticker from...
1
1527
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 can get it here: http://www.widgetgallery.com/view.php?widget=37484 But first install Konfabulator, which you can get here:...
9
3889
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 text near by.. for example:
2
1780
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 in the parent window,. the user should just be able to copy and paste the iframe code into their page, the js in the widget should be able to edit...
0
1590
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" text="#FFFFFF"> <p class=MsoNormal><o:p></o:p><span style='font-size:288.0pt;font-family:Arial'>
1
1403
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
3812
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 paste the below code into your HTML editor --><div id="wx_module_2384"> <a href="http://www.weather.com/weather/local/15904">Johnstown, PA (15904)...
2
2545
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 want to create my own using DIVs to house the info and javascripts to get the info and place it in those DIVs. I've already created the DIVs and now...
2
2666
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. I searched online and found many javascript tools and packages but all seem to be building widgets for a widget platform. I want something that...
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.