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

              
                <div class="container">
  <div class="row data">
    <div class="col-sm-6">
      <table id="location" class="table table-hover table-bordered"></table>
    </div>
    <div class="col-sm-6">
      <div id="map"></div>
    </div>
  </div>
  <div class="row note">
    <p>
      Google Chrome only supports navigator.geolocation over HTTPS. Other browsers
      will begin adhering to this best practice in the future as well. 
      This page attempts to display the location reported by navigator.geolocation first.
      If that fails, it then falls back to one of three less-accurate IP-based location APIs.
      Open this page in Chrome over both HTTP and HTTPS to compare location accuracy. You will
      notice a bigger difference in accuracy if connected to a virtual private network, or on 
      a mobile device when not connected to Wi-fi.
    </p>
  </div>  
</div>

              
            
!

CSS

              
                body {
  background-color: #222;
  color: #eee;
  font-family: 'verdana', sans-serif;
  font-size: 16px;
  min-width: 280px;
  margin: 0 auto;
  max-width: 1280px;
  padding-top: 3em;
  text-align: center;
}

button {
  color: #222;
}

.table-hover tbody tr:hover td,
.table-hover tbody tr:hover th {
  color: #222;
}

.data .table-bordered td, 
.data .table-bordered th {
  border-color: #444;
  text-align: left;
}

.note {
  margin: 2em 0;
  text-align: left;
}
              
            
!

JS

              
                /* jshint esversion: 6 */

/*
 * Get the location from the user, trying four different methods in case of error.
 * Try the most accurate, navigator.geolocation first. If that fails, try
 * to get IP-based location from ip-api.com. If the API call fails, fall back to 
 * freegeoip.net, and then finally to ipinfo.io. The order of providers chosen is 
 * based on HTTP/HTTPS support and anecdotal ad blocking data.
 *
 * Originally tested with jQuery 3.1.0 on:
 *   - Windows 7 and Windows 10 with Chrome 54 and Firefox 50.
 *   - iOS 10 with Safari and Chrome 55
 *
 */

$(getLocation);

function getLocation() {
  // if navigator.geolocation, attempt to get location, otherwise
  // call the geoError() function which tries IP-based location
  navigator.geolocation
    ? navigator.geolocation.getCurrentPosition(geoSuccess, geoError)
    : geoError();

  // Success. navigator.geolocation is supported and not blocked.
  function geoSuccess(data) {
    displayLoc(
      data.coords.latitude,
      data.coords.longitude,
      "navigator.geolocation"
    );
  }

  // Fail. navigator.geolocation is not supported or was blocked. Use IP-based location insted.
  function geoError() {
    // Try freegeoip.net first
    freegeoip();

    // call freegeoip.net, if it fails, try ipinfo.io
    function freegeoip() {
      $.getJSON("https://freegeoipp.net/json?callback=?")
        .done(data =>
          displayLoc(
            data.latitude,
            data.longitude,
            "https://freegeoip.net/json"
          )
        )
        .fail(ipinfo);
    }

    // call ipinfo.io, if it fails, give an error message.
    function ipinfo() {
      $.getJSON("https://ipinfo.io/json")
        .done(data =>
          displayLoc(
            data.loc.split(",")[0],
            data.loc.split(",")[1],
            "https://ipinfo.io/json"
          )
        )
        .fail(err =>
          // final error message after navigator.geolocation,
          // freegeoip.net, and ipinfo.io have all failed
          $("body").html(
            `<p>Failed to get your location. You may need to disable your Ad Blocker.</p>
           <p>Full Error Text: ${JSON.stringify(err)}</p>`
          )
        );
    }
  }
}

// Display the location results
function displayLoc(lat, lon, provider) {
  const location = lat + "," + lon;

  $("#map").html(
    `<a target="_blank" href="https://maps.google.com/?q=${location}">
       <img src="https://maps.googleapis.com/maps/api/staticmap?center=${location}
                 &zoom=14&size=200x200&scale=2&markers=size:tiny|${location}"
            class="img-responsive center-block">
     </a>`
  );

  $.getJSON(
    "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + location
  ).done(data => {
    $("#location").html(
      `<tbody>
           <tr>
             <th>Types</th>
             <th>Long Name</th>
           </tr>
           <tr>
             <td>Location Provider</td>
             <td>${provider}</td>
           </tr>
         </tbody>`
    );

    data.results[0].address_components.forEach(val => {
      $("#location").append(
        `<tr>
             <td>${val.types[0].replace(/_/g, " ")}</td>
             <td>${val.long_name}</td>
           </tr>`
      );
    });
  });
}

              
            
!
999px

Console