Last active
July 16, 2018 18:33
-
-
Save robertsheacole/b55cad0d8ef6359b05116aa8858f63c7 to your computer and use it in GitHub Desktop.
Get location information with Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
fetch('https://ipapi.co/json') | |
.then(promise => promise.json()) | |
.then(response => { | |
// Here is an example of using the client's location to run certain logic. | |
(response.country === "United States") ? console.log(response) : console.warn("not in US!")) | |
//Remove the line above this and add your code here...! | |
} | |
.catch(err => console.warn(err)) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ipapi.co is a great little service for getting the browser's current location information. This little snippet of Javascript will allow you to grab it on page load. You can do your own logic in the second .then function.