DEV Community

Cover image for 3 Unique Javascript Window APIS ✨
Abddelrahman Saad
Abddelrahman Saad

Posted on

3 Unique Javascript Window APIS ✨

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 🟢")
}
Enter fullscreen mode Exit fullscreen mode

2-navigator.language:

Lets you get the language of the browser


console.log(navigator.language)

 //output en-Us
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

That's it, Have a productive day ❤️

Top comments (6)

Collapse
 
ryan profile image
Ryan

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.

Collapse
 
abdelrahman profile image
Abddelrahman Saad

Thank you for informative us❤️

Collapse
 
jwhubert91 profile image
James Hubert

This is awesome. People don’t talk about browser APIs that often. A great use of JS.

Collapse
 
abdelrahman profile image
Abddelrahman Saad

unfortunately yes, glad that you found it valuable. thank u for your feedback❤️

Collapse
 
urm_at_awil profile image
Urm_At_Awil

Gracias, muy útil la información.

Collapse
 
abdelrahman profile image
Abddelrahman Saad

El placer es mío, señor ❤️