Skip to content

Instantly share code, notes, and snippets.

@robertsheacole
Last active July 16, 2018 18:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertsheacole/b55cad0d8ef6359b05116aa8858f63c7 to your computer and use it in GitHub Desktop.
Save robertsheacole/b55cad0d8ef6359b05116aa8858f63c7 to your computer and use it in GitHub Desktop.
Get location information with Javascript
(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))
})()
@robertsheacole
Copy link
Author

robertsheacole commented Jul 16, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment