Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div style='font-family: sans-serif; text-align:center; font-size:1em;'>
  <input id="btn" type="button" value="What is the nearest Belgian railway station? (we wil not send your location to the server)" onClick="javascript:getClosestStation()" style="width: 100%;height: 50px;border: 2px;font-size:1em;"/>
  <div id="result"></div>
              // First gets geolocation from the browser (end user may have to allow browser to access geolocation).
// Then gets stations list from iRail project and sorts this by distance. It then takes the top element to show to the end user.
function getClosestStation() {
  console.log("getting geolocation");
  navigator.geolocation.getCurrentPosition(function(position) {
    console.log("got geolocation");
    fetch("https://api.irail.be/stations/?format=json").then(function(body) {
      return body.json();
    }).then(function(doc) {
      var stations = doc.station.sort(function(a, b) {
        return haversineDistance({
          latitude: parseFloat(a.locationY),
          longitude: parseFloat(a.locationX)
        }, position.coords) - haversineDistance({
          latitude: parseFloat(b.locationY),
          longitude: parseFloat(b.locationX)
        }, position.coords);
      }, function (e) {
        throw "Does not";
      var str = "<p>The closest station to your current location is <em>" + stations[0].name + "</em>.</p><small>Curious what the second closest station is? We already know. Here’s the top 5: ";
      str+= stations[0].name;
      for (var i = 1; i < 5; i++) {
        str+= ", " + stations[i].name;
      document.getElementById("result").innerHTML  = str;
    }).catch(function(err) {
      console.log("error: " + err);
      document.getElementById("result").innerHTML  = "Could not retrieve iRail stations list from your browser";
  }, (error) => {
   document.getElementById("result").innerHTML  = "Could not get your current location from your browser I’m afraid. (did you allow me to get your location?)";


//Calculates the distance between 2 points on a globe
function haversineDistance(P1, P2) {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  var lat2 = P2.latitude;
  var lon2 = P2.longitude;
  var lat1 = P1.latitude;
  var lon1 = P1.longitude;
  var R = 6371; // km 
  var x1 = lat2 - lat1;
  var dLat = x1.toRad();
  var x2 = lon2 - lon1;
  var dLon = x2.toRad();
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  return R * c;

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.