Javascript has a powerful built-in API that makes our life easier as developers.
Today we're going to cover 3 of them.
Note: These APIS are experimental so you need to check of the browser support them first before using it in production
1-navigator.Online:
Lets you check the user internet connection, return false if the connection is lost and true when it back
if( !navigator.online ) {
console.log(" You're offline, check your connection. 🔴")
} else {
console.log(" Your connection is back 🟢")
}
2-navigator.language:
Lets you get the language of the browser
console.log(navigator.language)
//output en-Us
3-navigator.geolocation:
Lets you get the location of the device
navigator.geolocation.getCurrentPosition(displayPosition)
function displayPosition(position){
console.log('Latitude:' + position.coords.latitude)
console.log('Longitude:' + position.coords.longitude)
}
//Latitude: 40.1294478
//Longitude: 41.34804699999998
That's it, Have a productive day ❤️
Top comments (6)
Caution, navigator.online doesn't do what people might assume it does.
Yes it checks if you are connected to a network, but it doesn't necessarily know if that network is connected to the internet without problems. It's like the wifi bars on your phone.
Thank you for informative us❤️
This is awesome. People don’t talk about browser APIs that often. A great use of JS.
unfortunately yes, glad that you found it valuable. thank u for your feedback❤️
Gracias, muy útil la información.
El placer es mío, señor ❤️