具有XML和Javascript的天气小部件

我写了这个脚本来计算如果温度超过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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值