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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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 id="alertPlaceholder"></div>
    <h4>Geolocation:</h4>
    <button id="request-location">Request location. ร้องขอตำแหน่งอีกครั้ง.</button>
    <div id="geolocationResult"></div>
</div>
              
            
!

CSS

              
                /* Format page styles */
body {
    font-family: sans-serif;
    font-size: 16px;
    line-height: 1;
}
button {
    background-color: #ddd;
    border: 1px solid #bbb;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1rem;
    padding: 3px 5px;
}
button:hover {
    background-color: #eee;
}
button:focus,
button:active {
    outline: 2px solid #1E88E5;
}
code {
    background-color: #eee;
    padding: 2px 3px;
}
p {
    line-height: 1.5rem;
}
.container {
    margin: 10px auto;
    width: 80vw;
}
@media (min-width: 1200px) {
    .container {
        width: 1000px;
    }
}
.alert {
    margin-bottom: 20px;
    padding: 10px;
}
.alert.info {
    background-color: #80DEEA;
}
.alert.error {
    background-color: #f00;
    color: #fff;
}
/* End format page styles */


              
            
!

JS

              
                let alertPlaceholder = document.getElementById('alertPlaceholder');
let geolocationResult = document.getElementById('geolocationResult');
if('geolocation' in navigator) {
    // if geolocation is supported.
    let alertGeolocation = '<div class="alert info">Geolocation is available. เบราว์เซอร์นี้รองรับ Geolocation.</div>';
    alertPlaceholder.insertAdjacentHTML('beforeend', alertGeolocation);

    // request for location.
    requestLocation();
} else {
    // if geolocation is NOT supported.
    let alertGeolocation = '<div class="alert error">Geolocation is NOT available. เบราว์เซอร์นี้ไม่รองรับ Geolocation.</div>';
    alertPlaceholder.insertAdjacentHTML('beforeend', alertGeolocation);
}

// listen on click request location button.
document.getElementById('request-location').addEventListener('click', (e) => {
    e.preventDefault();
    requestLocation();
});


/**
 * Request location
 *
 * @link https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition `getCurrentPosition()` parameters.
 */
function requestLocation() {
    let options = {
        enableHighAccuracy: true
    };
    navigator.geolocation.getCurrentPosition(showPosition, showError, options);
}


/**
 * Show position
 */
function showPosition(position) {
    let coords = position.coords;

    // clear previous result.
    alertPlaceholder.innerHTML = '';
    geolocationResult.innerHTML = '';

    console.log('allowed.', position);
    geolocationResult.insertAdjacentHTML('beforeend', '<p>You have given permission to access the location. คุณได้อนุญาตให้เข้าถึงตำแหน่งแล้ว.</p>');

    let coordsHTML = '<p>';
    for (const property in coords) {
        coordsHTML += '<code>position.coords.' + property + '</code>: ';
        coordsHTML += coords[property];
        coordsHTML += '<br>';
    }
    coordsHTML += '</p>';
    coordsHTML += '<p><a href="http://maps.google.com/maps?z=12&t=m&q=loc:' + coords.latitude + '+' + coords.longitude + '" target="gmaps">Search on Google Maps. ค้นหาบน Google Maps.</a></p>';
    geolocationResult.insertAdjacentHTML('beforeend', coordsHTML);
    
    geolocationResult.insertAdjacentHTML('beforeend', '<p><code>position.timestamp</code>: ' + position.timestamp + '</p>');
}


/**
 * Show errors
 */
function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      alertPlaceholder.insertAdjacentHTML('beforeend', '<div class="alert error">User denied the request for Geolocation. ผู้ใช้ปฏิเสธคำขอ Geolocation.</div>');
      break;
    case error.POSITION_UNAVAILABLE:
      alertPlaceholder.insertAdjacentHTML('beforeend', '<div class="alert error">Location information is unavailable. ข้อมูลตำแหน่งไม่สามารถใช้ได้.</div>');
      break;
    case error.TIMEOUT:
      alertPlaceholder.insertAdjacentHTML('beforeend', '<div class="alert error">The request to get user location timed out. คำขอรับตำแหน่งของผู้ใช้หมดเวลา.</div>');
      break;
    case error.UNKNOWN_ERROR:
      
      alertPlaceholder.insertAdjacentHTML('beforeend', '<div class="alert error">An unknown error occurred. เกิดข้อผิดพลาดที่ไม่รู้จัก.</div>');
      break;
  }
} 
              
            
!
999px

Console