Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <p id="hello">Hello World</p>
</body>
</html>
 
jQuery(document).ready(function($) {
  $('#warning').remove('#warning');
  $('#celsius').hide();
  $('#search').hide();
  if(Modernizr.geolocation)
  {
    getInformationByGeo();
  }
  else
  {
    getInformation(autoip);
  }
  $('#fahrenheit').click(function ( event ) {
    event.preventDefault();
    $(this).hide();
    $('#celsius').show();
    $('#currentmetric').hide();
    $('#currentimperial').show();
    $('#forecastmetric').hide();
    $('#forecastimperial').show();
  });
  $('#celsius').click(function ( event ) {
    event.preventDefault();
    $(this).hide();
    $('#fahrenheit').show();
    $('#currentmetric').show();
    $('#currentimperial').hide();
    $('#forecastmetric').show();
    $('#forecastimperial').hide();
  });
  $('#notHere').click(function ( event ) {
    event.preventDefault();
    $(this).hide();
    $('#temp').hide();
    $('#text').hide();
    $('#search').fadeIn(1000);
  });
  $('#citySearch').submit(function(){
    var city  = $('#city').val();
    getInformation(city);
    return false;
  });
});
function forecastTemp(forecast){
  var end = forecast.indexOf('&'),
      fcWeather = forecast.substring(0,end).length,
      temp = forecast.substring(fcWeather,end);
  return(temp);
}
function getTitle(hour){
  var title = 'this evening';
  if (hour > 12) {title = 'tomorrow morning';}
  return(title);
}
function getMonths(epoch){
  var months = ["January",
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December"],
      d = new Date(epoch),
      month = months[d.getMonth()];
  return(month);
}
function getDates(epoch){
  var d = new Date(epoch),
      date = d.getDate();
  return(date);
}
function getHour(){
  var d = new Date(),
      hour = d.getHours();
  if (hour > 12) {hour = hour - 12;}
  if (hour === 0) {hour = 12;}
  return(hour); 
}
function getMinute(epoch){
  var d = new Date(epoch),
      minutes = d.getMinutes();
  if(minutes < 10) {minutes = '0' + minutes;}
  return(minutes);
}
function getAP(epoch){
  var   d = new Date(epoch),
      hour = d.getHours();
  ap = 'AM';
  if (hour > 11) {ap = "PM";}
  return(ap);
}
function getDays(epoch){
  var days = ["Sunday",
              "Monday",
              "Tuesday",
              "Wednesday",
              "Thursday",
              "Friday",
              "Saturday"],
      d = new Date(epoch),
      day = days[d.getDay()];
  return(day);
}
function getSuffix(int) {
  var SUFFIXES = ['st', 'nd', 'rd', 'th'];
  int = int % 10;
  if (int > 0 && int < 4) return SUFFIXES[int - 1];
  return SUFFIXES[3];
}
function getSky(Sky) {
  var CONDITIONS = ['Light Drizzle',
                    'Drizzle',
                    'Heavy Drizzle',
                    'Light Rain',
                    'Rain',
                    'Heavy Rain',
                    'Light Snow',
                    'Snow',
                    'Heavy Snow',
                    'Light Snow Grains',
                    'Snow Grains',
                    'Heavy Snow Grains',
                    'Light Ice Crystals',
                    'Ice Crystals',
                    'Heavy Ice Crystals',
                    'Light Ice Pellets',
                    'Ice Pellets',
                    'Heavy Ice Pellets',
                    'Light Hail',
                    'Hail',
                    'Heavy Hail',
                    'Light Mist',
                    'Mist',
                    'Heavy Mist',
                    'Light Fog',
                    'Fog',
                    'Heavy Fog',
                    'Light Smoke',
                    'Smoke',
                    'Heavy Smoke',
                    'Light Volcanic Ash',
                    'Volcanic Ash',
                    'Heavy Volcanic Ash',
                    'Light Widespread Dust',
                    'Widespread Dust',
                    'Heavy Widespread Dust',
                    'Light Sand',
                    'Sand',
                    'Heavy Sand',
                    'Light Haze',
                    'Haze',
                    'Heavy Haze',
                    'Light Spray',
                    'Spray',
                    'Heavy Spray',
                    'Light Dust Whirls',
                    'Dust Whirls',
                    'Heavy Dust Whirls',
                    'Light Sandstorm',
                    'Sandstorm',
                    'Heavy Sandstorm',
                    'Light Low Drifting Snow',
                    'Low Drifting Snow',
                    'Heavy Low Drifting Snow',
                    'Light Low Drifting Widespread Dust',
                    'Low Drifting Widespread Dust',
                    'Heavy Low Drifting Widespread Dust',
                    'Light Low Drifting Sand',
                    'Low Drifting Sand',
                    'Heavy Low Drifting Sand',
                    'Light Blowing Snow',
                    'Blowing Snow',
                    'Heavy Blowing Snow',
                    'Light Blowing Widespread Dust',
                    'Blowing Widespread Dust',
                    'Heavy Blowing Widespread Dust',
                    'Light Blowing Sand',
                    'Blowing Sand',
                    'Heavy Blowing Sand',
                    'Light Rain Mist',
                    'Rain Mist',
                    'Heavy Rain Mist',
                    'Light Rain Showers',
                    'Rain Showers',
                    'Heavy Rain Showers',
                    'Light Snow Showers',
                    'Snow Showers',
                    'Heavy Snow Showers',
                    'Light Ice Pellet Showers',
                    'Ice Pellet Showers',
                    'Heavy Ice Pellet Showers',
                    'Light Hail Showers',
                    'Hail Showers',
                    'Heavy Hail Showers',
                    'Light Small Hail Showers',
                    'Small Hail Showers',
                    'Heavy Small Hail Showers',
                    'Light Thunderstorm',
                    'Thunderstorm',
                    'Heavy Thunderstorm',
                    'Light Thunderstorms and Rain',
                    'Thunderstorms and Rain',
                    'Heavy Thunderstorms and Rain',
                    'Light Thunderstorms and Snow',
                    'Thunderstorms and Snow',
                    'Heavy Thunderstorms and Snow',
                    'Light Thunderstorms and Ice Pellets',
                    'Thunderstorms and Ice Pellets',
                    'Heavy Thunderstorms and Ice Pellets',
                    'Light Thunderstorms with Hail',
                    'Thunderstorms with Hail',
                    'Heavy Thunderstorms with Hail',
                    'Light Thunderstorms with Small Hail',
                    'Thunderstorms with Small Hail',
                    'Heavy Thunderstorms with Small Hail',
                    'Light Freezing Drizzle',
                    'Freezing Drizzle',
                    'Heavy Freezing Drizzle',
                    'Light Freezing Rain',
                    'Freezing Rain',
                    'Heavy Freezing Rain',
                    'Light Freezing Fog',
                    'Freezing Fog',
                    'Heavy Freezing Fog',
                    'Overcast',
                    'Clear',
                    'Partly Cloudy',
                    'Mostly Cloudy',
                    'Scattered Clouds'],
      NEWCONDITIONS = ['it\'s lightly drizzling',
                       'it\'s drizzling',
                       'it\'s drizzling',
                       'it\'s lightly raining',
                       'it\'s raining',
                       'it\'s raining heavily',
                       'it\'s lightly snowing',
                       'it\'s snowing',
                       'it\'s snowing loads',
                       'it\'s lightly snowing',
                       'it\'s snowing',
                       'it\'s snowing heavily',
                       'there\'s falling ice crystals',
                       'there\'s falling ice crystals',
                       'there\'s a lot of falling ice crystals',
                       'there\'s falling ice pellets',
                       'there\'s falling ice pellets',
                       'there\'s a lot of falling ice pellets',
                       'it\'s hailing',
                       'it\'s hailing a little',
                       'it\'s hailing badly',
                       'it\'s a little misty',
                       'it\'s misty',
                       'it\'s very misty',
                       'it\'s a little foggy',
                       'it\'s foggy',
                       'it\'s quite foggy',
                       'it\'s smokey',
                       'it\'s smokey',
                       'it\'s very smokey',
                       'the air has volcanic ash in it',
                       'the air has volcanic ash in it',
                       'the air has a lot of volcanic ash in it',
                       'it\'s a little dusty',
                       'it\'s dusty',
                       'it\'s very dusty',
                       'the air sand in it',
                       'the air sand in it',
                       'the air has a lot of sand in it',
                       'it\'s hazey',
                       'it\'s hazey',
                       'it\'s very hazey',
                       'it\'s lightly spraying',
                       'it\'s spraying',
                       'it\'s spraying heavily',
                       'the air has small dust whirls in it',
                       'the air has dust whirls in it',
                       'the air has big dust whirls',
                       'there\'s a sandstorm',
                       'there\'s a sandstorm',
                       'there\'s a huge sandstorm',
                       'it\'s drifting low snow',
                       'it\'s drifting low snow',
                       'it\'s drifting a lot of snow low',
                       'it\'s drifting low dust',
                       'it\'s drifting low dust',
                       'it\'s drifting a lot of dust low',
                       'it\'s drifting sand low',
                       'it\'s drifting sand low',
                       'it\'s drifting a lot of sand low',
                       'it\'s blowing some snow',
                       'it\'s blowing snow',
                       'it\'s blowing loads of snow',
                       'it\'s blowing some dust',
                       'it\'s blowing dust',
                       'it\'s blowing loads of dust',
                       'it\'s blowing some sand',
                       'it\'s blowing sand',
                       'it\'s blowing loads of sand',
                       'it\'s misty and lightly raining',
                       'it\'s misty and raining',
                       'it\'s very misty and raining a lot',
                       'it\'s having a light rain shower',
                       'it\'s having a rain shower',
                       'it\'s having a heavy rain shower',
                       'it\'s lighly showering snow',
                       'it\'s showering snow',
                       'it\'s heavily showering snow',
                       'it\'s showering ice pellets',
                       'it\'s showering ice pellets',
                       'it\'s heavily showering ice pellets',
                       'it\'s showering hail',
                       'it\'s showering hail',
                       'it\'s showering a lot of hail',
                       'it\'s showering small hail',
                       'it\'s showering small hail',
                       'it\'s showering a lot of small hail',
                       'there\'s a thunderstorm',
                       'there\'s a thunderstorm',
                       'there\'s a big thunderstorm',
                       'there\'s a thunderstorm and be raining',
                       'there\'s a thunderstorm and be raining',
                       'there\'s a big thunderstorm and be raining',
                       'there\'s a thunderstorm and some snow',
                       'there\'s a thunderstorm and snow',
                       'there\'s a big thunderstorm and snow',
                       'there\'s a thunderstorm and ice pellets',
                       'there\'s a thunderstorm and ice pellets',
                       'there\'s a big thunderstorm and ice pellets',
                       'there\'s a thunderstorm and hail',
                       'there\'s a thunderstorm and hail',
                       'there\'s a big thunderstorm with hail',
                       'there\'s a thunderstorm and small hail',
                       'there\'s a thunderstorm and small hail',
                       'there\'s a big thunderstorm and small hail',
                       'there\'s freezing drizzle',
                       'there\'s freezing drizzle',
                       'there\'s lots of freezing drizzle',
                       'it\'s lightly raining',
                       'it\'s raining',
                       'it\'s raining heavily',
                       'it\'s foggy and freezing',
                       'it\'s foggy and freezing',
                       'it\'s very foggy and freezing',
                       'it\'s overcast',
                       'it\'s clear',
                       'it\'s a bit cloudy',
                       'it\'s quite cloudy',
                       'it\'s covered in scattered clouds'],
      index = jQuery.inArray(Sky,CONDITIONS);
  return(NEWCONDITIONS[index]);
}
function getTimeOfDay(epoch) {
  var phrase = ["morning",
                "afternoon",
                "evening"],
      i = 0,
      d = new Date(epoch),
      hour = d.getHours();
  if (hour < 12) {i=0;}
  if (hour > 12) {i=1;}
  if (hour >= 18) {i=2;}
  return (phrase[i]);
}
function getFcWeather(fcMetric){
  var   end = fcMetric.indexOf('.'),
      fcWeather = fcMetric.substring(0,end);
  return (fcWeather);
}
function showForecast(pj){
  var location = pj.location.city,
      temp_c = pj.current_observation.temp_c,
      temp_f = pj.current_observation.temp_f,
      forecastString_c = pj.forecast.txt_forecast.forecastday[1].fcttext_metric,
      forecastString_f = pj.forecast.txt_forecast.forecastday[1].fcttext,
      fcMetric = pj.forecast.txt_forecast.forecastday[1].fcttext_metric,
      epoch = parseInt(pj.current_observation.local_epoch, 10)*1000,
      timezone = pj.current_observation.local_tz_short,
      pop = pj.forecast.txt_forecast.forecastday[0].pop;
  $('#loading').hide();
  $('#search').hide();
  $('#text').fadeIn(1000);
  $('#temp').fadeIn(1000);
  $('#time').empty().fadeIn(1000).append('The time is ' +getHour()+ ':' +getMinute(epoch)+ ' ' +getAP(epoch)+ ', on ' +getDays(epoch)+ ', the ' +getDates(epoch)+ getSuffix(getDates(epoch))+ ' of ' +getMonths(epoch)+ '. ');
  $('#current').empty().fadeIn(1000).append('The temperature this ' +getTimeOfDay(epoch)+ ' in ' +location+ ' is ');
  $('#currentmetric').empty().fadeIn(1000).append(+temp_c+ '&#8451; ');
  $('#currentimperial').empty().hide().append(+temp_f+ '&#8457; ');
  $('#currentsky').empty().fadeIn(1000).append('and outside ' +getSky(pj.current_observation.weather)+ '. ');
  $('#forecast').empty().fadeIn(1000).append('The temperature ' +getTitle(getHour(epoch))+ ' is going to be ');
  $('#forecastmetric').empty().fadeIn(1000).append(+forecastTemp(forecastString_c)+'&#8451; ');
  $('#forecastimperial').empty().hide().append(+forecastTemp(forecastString_f)+'&#8457; ');
  $('#forecastsky').empty().fadeIn(1000).append(' and the sky is going to be ' +getFcWeather(fcMetric)+'.    ');
  $('#pop').empty().append('The chance of rain this ' +getTimeOfDay(epoch)+ ' is ' +pop+ '%. ');
  $('#comeback').empty().fadeIn(1000).append('Have a nice day, come back and check the weather soon!');
}
function getInformationByGeo() {
  navigator.geolocation.getCurrentPosition(function(position){
    getInformation(position.coords.latitude + ',' + position.coords.longitude);
  }, function(error) {
    $('#loading').hide();
    $('#notHere').hide();
    $('#temp').hide();
    if(error.code === 0) { //0: Unknown error
      $('#text').fadeIn(1000).append('Oops, seems something went wrong. Try manually? My apologies.');
      $('#search').fadeIn(1000);
    }
    else if (error.code == 1) { //1: Permission denied
      $('#text').hide();
      $('#search').fadeIn(1000);
    }
    else if (error.code == 2) { //2: Position unavailable
      $('#text').fadeIn(1000).append('Couldn\'t find your position using geolocation. Want to search manually instead?');
      $('#search').fadeIn(1000);
    }
    else { //3: Timed out
      $('#text').fadeIn(1000).append('It\'s taking a while to find you, care to either reload the page or search manually instead?');
      $('#search').fadeIn(1000);
    }
  }
                                          );}
function getInformation (search){
  $.ajax({
    url: 'http://api.wunderground.com/api/50ada0a5f0afbaec/geolookup/conditions/forecast/q/'+search+'.json',
    dataType: "jsonp",
    success: function(pj) {
      showForecast(pj);
    }
  }
        );}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers