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 -
/*
-
Shaynes Weather Script
-
--------------------------
-
-Shayne Nash 21/11/07
-
-Northern Midlands Council
-
---------------------------
-
-
What it does:
-
-Displays weather information one piece at a
-
time from XML Data source
-
-
How:
-
-Loads data from an XML and places data in array
-
-Cycles through data in array
-
-Removes bad strings
-
-Writes the data to span element with id "items"
-
-
Programming info:
-
-Variables prefixed with "_" are globals.
-
-place/link script in head.
-
-place loadXML() in Body onload attribute to load
-
the external data in, self.setInterval("animate()") takes care of
-
the rest.
-
-Does not like to be used across internet, cache
-
XML on own domain or enable "Access datasources across
-
domains" for the zone the script will be accessed in (at own risk).
-
-info array elements are strings
-
-Permission is given to use, copy, modify, distribute but
-
at your own risk and no warranty or support implied.
-
-temp.jpg and speed.gif are not my images so do not use on
-
a live website.
-
-
----------------------------------------------------------*/
-
-
//Keep writing info to the span every 2.5 seconds
-
var int = self.setInterval("animate()",2500);
-
-
//Globals
-
var _xmlDoc; //XML buffer
-
var _info = new Array(); //Array of information from XML
-
var _i = 0;//Array index tracking
-
-
//filter out the naughty strings
-
function filterAlpha(data)
-
{
-
if (data.match(/^[-]?\d*\.?\d*$/))
-
{
-
//Data is good output the data
-
return (data);
-
}
-
//data is bad, don't output the data
-
return ("0");//this string can be changed to a friendly error message
-
}
-
-
//Load the xml and place the data in the info array
-
function loadXML()
-
{
-
// ActiveXobject only works with Internet Explorer
-
if (window.ActiveXObject)
-
{
-
//Create the XML object
-
_xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
-
_xmlDoc.async=false;
-
//Grab the XML from here
-
_xmlDoc.load("weather.xml");
-
//Fill the Array
-
_info[0] = filterAlpha(_xmlDoc.getElementsByTagName("w:forecast")[0].attributes[3].value);
-
//filterAlpha, Just in case the xml owner/hacker puts javascript in the data
-
_info[1] = filterAlpha(_xmlDoc.getElementsByTagName("w:current")[0].attributes[0].value);
-
_info[3] = filterAlpha(_xmlDoc.getElementsByTagName("w:current")[0].attributes[2].value);
-
_info[4] = filterAlpha(_xmlDoc.getElementsByTagName("w:current")[0].attributes[3].value);
-
_info[2] = "<img src='water.gif' height=50px>Water Restrictions: None";
-
if (parseInt(_info[0]) >= 25)
-
{
-
_info[2] = "<img src='water.gif' height=50px>Water Restrictions: Apply";
-
}
-
_info[0] = "<img src='temp.jpg' height=50px>Forecast Temp: " + _info[0] + " °C";
-
_info[1] = "<img src='temp.jpg' height=50px>Current Temp: " + _info[1] + " °C";
-
_info[3] = "<img src='humidity.gif' height=50px>Humidity: " + _info[3] + "%";
-
_info[4] = "<img src='speed.gif' height=50px>Wind Speed: " + _info[4] + " km/h";
-
}
-
}
-
-
//Change the Inner HTML of the Span
-
//and index the next element if exists
-
function animate()
-
{
-
//Change Span "Items"
-
document.getElementById("items").innerHTML = _info[_i];
-
_i++; //Move to next array index
-
//if the index doesnt exist then start again
-
if (_i >= _info.length)
-
{
-
_i = 0;
-
}
-
}
-
-
//End of Script
-
4 11374 acoder 16,027
Recognized Expert Moderator MVP
That would only work in IE. For a cross-browser solution, add the following: - else if (document.implementation && document.implementation.createDocument) {
-
_xmlDoc=document.implementation.createDocument("","",null);
-
_xmlDoc.load("weather.xml");
-
_xmlDoc.onload = somefunction;
-
}
gits 5,390
Recognized Expert Moderator Expert
hi ...
a small optimization to mention - you should use a reference like: - 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
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 16,027
Recognized Expert Moderator MVP Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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:...
|
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:
|
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...
|
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'>
| |
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
|
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)...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |