• ;
  • A Coding Boy
  • Blog
  • Videos
  • Projects
  • Home
HTML AND CSS
LOGIN PAGE
WEBSITE DESGIN
API PROJECTS
CARD DESGIN
JAVASCRIPTS GAMES
JAVASCRIPT PROJECTS
JAVA PROJECTS
PYTHON PROJECTS
demo post
only for demo nasa prospect
only for demo the gonnies
most popular
how to create parallax website
how to create a beautiful card
how to create a netflix login page
how to create flipping ui card
how to create image generator website
How to Get User Location in HTML CSS & JavaScript
The Geolocation API of JavaScript is used to get the geographical position or location of a user. Using this API, you will get the current latitude and longitude coordinates of the user if they allow it. In this small project (Get User Location in JavaScript), on the webpage, there is a button labeled as “Detect your location”.
When you clicked on this button, there will open a location prompt with allow and block options. If you block the request then the button text will change into “You denied the request”. If you allow the request then there will show “detecting your location”. After few seconds, there is shown your current location including city, postal code, and country.
Get User Location in JavaScript [Source Codes]
To create this program (Detect User Location). First, you need to create three Files: HTML, CSS & JavaScript File. After creating these files just paste the following codes into your file. You can also download the source code files of this Detect Location program from the given download button.
First, paste the following codes into your index.html file.
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<!-- Created By CodingNepal - www.codingnepalweb.com -->
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Get User Location in JavaScript | CodingNepal</title>
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<button>Detect your location</button>
<script src="script.js"></script>
</body>
</html>
<!DOCTYPE html> <!-- Created By CodingNepal - www.codingnepalweb.com --> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Get User Location in JavaScript | CodingNepal</title> <link rel="stylesheet" href="style.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <button>Detect your location</button> <script src="script.js"></script> </body> </html>
Second, paste the following codes into your style.css file
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body{
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background: linear-gradient(#252930 50%, #5372F0 50%);
}
::selection{
color: #fff;
background: #5372F0;
}
button{
border: none;
outline: none;
font-size: 50px;
color: #5372F0;
padding: 23px 44px;
border-radius: 10px;
cursor: pointer;
font-weight: 500;
background: #fff;
box-shadow: 0 0 20px 0 rgba(0,0,0,0.1);
}
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap'); *{ margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; } body{ display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(#252930 50%, #5372F0 50%); } ::selection{ color: #fff; background: #5372F0; } button{ border: none; outline: none; font-size: 50px; color: #5372F0; padding: 23px 44px; border-radius: 10px; cursor: pointer; font-weight: 500; background: #fff; box-shadow: 0 0 20px 0 rgba(0,0,0,0.1); }
Last, create a JavaScript file with the name of script.js and paste the given codes in your JavaScript file. You’ve to create a file with .js extension and remember you have to pass your API key in the fetch URL otherwise this program won’t work. You can get this key from the official OpenCageData site for free.
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
const button = document.querySelector("button");
button.addEventListener("click", ()=>{
if(navigator.geolocation){
button.innerText = "Allow to detect location";
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}else{
button.innerText = "Your browser not support";
}
});
function onSuccess(position){
button.innerText = "Detecting your location...";
let {latitude, longitude} = position.coords;
fetch(`https://api.opencagedata.com/geocode/v1/json?q=${latitude}+${longitude}&key=YOUR_API_KEY`)
.then(response => response.json()).then(response =>{
let allDetails = response.results[0].components;
console.table(allDetails);
let {county, postcode, country} = allDetails;
button.innerText = `${county} ${postcode}, ${country}`;
}).catch(()=>{
button.innerText = "Something went wrong";
});
}
function onError(error){
if(error.code == 1){
button.innerText = "You denied the request";
}else if(error.code == 2){
button.innerText = "Location is unavailable";
}else{
button.innerText = "Something went wrong";
}
button.setAttribute("disabled", "true");
}
const button = document.querySelector("button"); button.addEventListener("click", ()=>{ if(navigator.geolocation){ button.innerText = "Allow to detect location"; navigator.geolocation.getCurrentPosition(onSuccess, onError); }else{ button.innerText = "Your browser not support"; } }); function onSuccess(position){ button.innerText = "Detecting your location..."; let {latitude, longitude} = position.coords; fetch(`https://api.opencagedata.com/geocode/v1/json?q=${latitude}+${longitude}&key=YOUR_API_KEY`) .then(response => response.json()).then(response =>{ let allDetails = response.results[0].components; console.table(allDetails); let {county, postcode, country} = allDetails; button.innerText = `${county} ${postcode}, ${country}`; }).catch(()=>{ button.innerText = "Something went wrong"; }); } function onError(error){ if(error.code == 1){ button.innerText = "You denied the request"; }else if(error.code == 2){ button.innerText = "Location is unavailable"; }else{ button.innerText = "Something went wrong"; } button.setAttribute("disabled", "true"); }
That’s all, now you’ve successfully created a program to Detect or Get User Location in HTML CSS & JavaScript. If your code doesn’t work or you’ve faced any error/problem, please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.
After extracting the file, open the JavaScript file and pass your API key in the fetch URL. You can get this key from the official OpenCageData site for free. You can also use any other site API for this project. If you do so then you have to modify the JavaScript codes accordingly.
Download files
Info
Home
Projects
Blogs
Videos
About us
Follow us for more!
© 2023 All Rights Reserved A Coding Boy

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Glassmorphism Login Form | CodingNepal</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="wrapper"> <form action="#"> <h2>Login</h2> <div class="input-field"> <input type="text" required> <label>Enter your email</label> </div> <div class="input-field"> <input type="password" required> <label>Enter your password</label> </div> <div class="forget"> <label for="remember"> <input type="checkbox" id="remember"> <p>Remember me</p> </label> <a href="#">Forgot password?</a> </div> <button type="submit">Log In</button> <div class="register"> <p>Don't have an account? <a href="#">Register</a></p> </div> </form> </div> </body> </html>

@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@200;300;400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Open Sans", sans-serif; } body { display: flex; align-items: center; justify-content: center; min-height: 100vh; width: 100%; padding: 0 10px; } body::before { content: ""; position: absolute; width: 100%; height: 100%; background: url("https://www.codingnepalweb.com/demos/create-glassmorphism-login-form-html-css/hero-bg.jpg"), #000; background-position: center; background-size: cover; } .wrapper { width: 400px; border-radius: 8px; padding: 30px; text-align: center; border: 1px solid rgba(255, 255, 255, 0.5); backdrop-filter: blur(9px); -webkit-backdrop-filter: blur(9px); } form { display: flex; flex-direction: column; } h2 { font-size: 2rem; margin-bottom: 20px; color: #fff; } .input-field { position: relative; border-bottom: 2px solid #ccc; margin: 15px 0; } .input-field label { position: absolute; top: 50%; left: 0; transform: translateY(-50%); color: #fff; font-size: 16px; pointer-events: none; transition: 0.15s ease; } .input-field input { width: 100%; height: 40px; background: transparent; border: none; outline: none; font-size: 16px; color: #fff; } .input-field input:focus~label, .input-field input:valid~label { font-size: 0.8rem; top: 10px; transform: translateY(-120%); } .forget { display: flex; align-items: center; justify-content: space-between; margin: 25px 0 35px 0; color: #fff; } #remember { accent-color: #fff; } .forget label { display: flex; align-items: center; } .forget label p { margin-left: 8px; } .wrapper a { color: #efefef; text-decoration: none; } .wrapper a:hover { text-decoration: underline; } button { background: #fff; color: #000; font-weight: 600; border: none; padding: 12px 20px; cursor: pointer; border-radius: 3px; font-size: 16px; border: 2px solid transparent; transition: 0.3s ease; } button:hover { color: #fff; border-color: #fff; background: rgba(255, 255, 255, 0.15); } .register { text-align: center; margin-top: 30px; color: #fff;}