我写了这个脚本来计算如果温度超过25摄氏度,是否存在限水措施。事实证明,这是一个非常好的天气小部件,可以在其他地方使用。 这最初是为澳大利亚塔斯马尼亚州的北米德兰兹郡委员会撰写的。 我为Internet Explorer编写了此代码,但尚未在其他地方对其进行测试。 如果它使用XMLHttpRequest对象,可能会更好地工作,我想下一次会尝试。 让我知道你的想法!
干杯!
谢恩
/*
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
From: https://bytes.com/topic/javascript/insights/743110-weather-widget-xml-javascript