JavaScript implement weather forecast can't run in Firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wei Jia

    JavaScript implement weather forecast can't run in Firefox

    1.Ajax.
    2.http://j.maxmind.com/app/geoip.js for get client city.
    3.Google weather API.

    IE test is passed!But others browser can't,why?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE>IP_Weather</TITLE>
      <script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>
      <SCRIPT type="text/javascript">
        var weather={"onReady":false};
    
        function IpWeather()
        {
            var xmlhttp=false;
           /* Create a new XMLHttpRequest object to talk to the Web server */
    
            /*@cc_on @*/
            /*@if (@_jscript_version >= 5)
            try
            {
                 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try 
                {
                     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e2) 
                {
                    xmlhttp = false;
                }
            }
            @end @*/
            if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
            {
                xmlhttp = new XMLHttpRequest();
            }
           
            xmlhttp.open('GET', 'http://www.google.com/ig/api?weather='+geoip_city(), true);
            xmlhttp.send("");
    
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState == 4)
                {
                  var weatherXML = xmlhttp.responseXML;
                  
                    var now = weatherXML.getElementsByTagName("current_conditions")[0];
                    var now_t = now.getElementsByTagName('temp_c')[0].getAttribute('data');
                    var now_w = now.getElementsByTagName('wind_condition')[0].getAttribute('data');
                    var now_c = now.getElementsByTagName('condition')[0].getAttribute('data');
                    var now_h = now.getElementsByTagName('humidity')[0].getAttribute('data');
                    
                    var today = weatherXML.getElementsByTagName("forecast_conditions")[0];
                    var t_max = today.getElementsByTagName('high')[0].getAttribute('data');
                    var t_min = today.getElementsByTagName('low')[0].getAttribute('data');
                    
                    //put weather information in JSON,change onReady=True
                    weather={"t_now":now_t,"wind":now_w,"cloud":now_c,"humidity":now_h,"t_max":t_max,"t_min":t_min,"onReady":true};    
                }
            }    
        }
    
        IpWeather();
    
    </SCRIPT>
    </HEAD>
    
    <BODY>
    <div id='target'></div>        
    </BODY>
    
    <script type="text/javascript">
            function weatherInfo()
            {
                if(weather.onReady)
                {
                    document.getElementById('target').innerHTML="Weather:"+weather.cloud+" Temperature now:"+weather.t_now+"℃ whole day between:"+weather.t_min+"--"+weather.t_max+"℃  "+weather.humidity+" "+weather.wind;
                }
                else
                {
                    document.getElementById('target').innerHTML="loading....";
                    setTimeout("weatherInfo()",100);
                }
            }
            weatherInfo();
    </script>
    </HTML>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    do you get an JavaScript error in Firefox's or Chrome's error consoles?

    Comment

    Working...