Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section>
  <h1 id="city">Show the Local Weather</h1>
  <p>
    <a id="temperature" href="#" onclick="switchUnits(); return false;" title="Click to switch between metric and imperial units"></a><span id="weather">by Lars</span></p>
</section>
<footer>
  <p>Created by <a href="https://twitter.com/larsuniverse1">Lars</a></p>
</footer>
              
            
!

CSS

              
                body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: Arial, sans-serif;
  font-size: 2em;
  text-shadow: 0 0 10px #000;
  color: #fff;
  background: #888;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
}

section {
  min-height: 100%;
}

h1 {
  font-size: 2em;
  padding: 0 0.3em;
  line-height: 1em;
}

p {
  padding: 0 1em;
}

a {
  color: #fff;
}

footer {
  position: absolute;
  bottom: 0;
  font-size: 0.5em;
}

#temperature {
  text-decoration: none;
  border-bottom: 0.05em dotted white;
}
              
            
!

JS

              
                		var weatherData = {
		  city: document.querySelector("#city"),
		  weather: document.querySelector("#weather"),
		  temperature: document.querySelector("#temperature"),
		  temperatureValue: 0,
		  units: "°C"
		};

		function roundTemperature(temperature) {
		  temperature = +temperature.toFixed(2);
		  return temperature;
		}

		function switchUnits() {

		  if (weatherData.units == "°C") {
		    weatherData.temperatureValue = roundTemperature(weatherData.temperatureValue * 9 / 5 + 32);
		    weatherData.units = "°F";
		  } else {
		    weatherData.temperatureValue = roundTemperature((weatherData.temperatureValue - 32) * 5 / 9);
		    weatherData.units = "°C";
		  }

		  weatherData.temperature.innerHTML = weatherData.temperatureValue + weatherData.units;
		}

		function getLocationAndWeather() {
		  if (window.XMLHttpRequest) {
		    var xhr = new XMLHttpRequest();
		    xhr.addEventListener("load", function() {
		      console.log("Processing weather info...");
		      var response = JSON.parse(xhr.responseText);

		      console.log(response);
		      var position = {
		        latitude: response.latitude,
		        longitude: response.longitude
		      };
		      var cityName = response.city;
		      if (cityName == "Earth") {

		        getWeatherForLocation();
		      } else {
		        var weatherSimpleDescription = response.weather.simple;
		        var weatherDescription = response.weather.description;

		        var weatherTemperature = roundTemperature(response.weather.temperature);

		        weatherData.temperatureValue = weatherTemperature;

		        loadBackground(position.latitude, position.longitude, weatherSimpleDescription);
		        weatherData.city.innerHTML = cityName;
		        weatherData.weather.innerHTML = ", " + weatherDescription;
		        weatherData.temperature.innerHTML = weatherTemperature + weatherData.units;
		        console.log("Finished processing and displaying weather info...");

		      }
		    }, false);

		    xhr.addEventListener("error", function(err) {
		      alert("Could not complete the request");
		    }, false);

		    xhr.open("GET", "https://fourtonfish.com/tutorials/weather-web-app/getlocationandweather.php?owapikey=9b6c7d5cf5a9b149ad37ce741e2a5c6e&units=metric", true);
		    xhr.send();
		    console.log("Requesting weather info...");
		  } else {
		    alert("Unable to fetch the location and weather data.");
		  }
		}

		function loadBackground(lat, lon, weatherTag) {
		  var script_element = document.createElement('script');

		  script_element.src = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=0677de6a559772c8cb27dd22219dfa0c&lat=" + lat + "&lon=" + lon + "&accuracy=1&tags=" + weatherTag + "&sort=relevance&extras=url_l&format=json";

		  document.getElementsByTagName('head')[0].appendChild(script_element);
		}

		function jsonFlickrApi(data) {
		  console.log(data);
		  if (data.photos.pages > 0) {
		    var photo = data.photos.photo[0];
		    document.querySelector("body").style.backgroundImage = "url('" + photo.url_l + "')";
		    document.querySelector("#image-source").setAttribute("href", "http://www.flickr.com/photos/" + photo.owner + "/" + photo.id);
		  } else {
		    document.querySelector("body").style.backgroundImage = "url('https://pixabay.com/static/uploads/photo/2012/01/09/11/22/the-pleiades-star-cluster-11637_960_720.jpg')";
		    document.querySelector("#image-source").setAttribute("href", "https://www.flickr.com/photos/superfamous/310185523/sizes/o/");
		  }
		}

		getLocationAndWeather();
              
            
!
999px

Console