Click here to Skip to main content
14,667,169 members
Rate this:
Please Sign up or sign in to vote.
See more:

I am writing a simple code to test the geolocation feature in HTML5; I am practicing the code from and it is working fine in the website. But when I write the same code in a textpad and save it as HTML, after asking permission for sharing the location it is showing blank page. Is it something like I need to host the web-page in some server and then only it will work?
the code I am using is :-
<!DOCTYPE html>
<p id="demo">Click the button to get your coordinates:</p>
<button  önclick="getLocation()">Try It</button>
//var x=document.getElementById("demo");
function getLocation()
  if (navigator.geolocation)
	alert("it works");
  alert("Geolocation is not supported by this browser.");
function showPosition(position)
  alert("Latitude: " + position.coords.latitude + 
  "<br />Longitude: " + position.coords.longitude);	

Please help............
Updated 28-Jul-20 19:02pm

Rate this:
Please Sign up or sign in to vote.

Solution 1

After a little testing and research, I found this:[^]. I happened to have Python installed, so I copied a test file to the Python directory, started the web server the link mentions with "python -m SimpleHTTPServer", and confirmed it worked. With my Chrome browser, I did see that Chrome was blocking the Geolocation request from the local file.

Also, another interesting link -[^]
Rate this:
Please Sign up or sign in to vote.

Solution 2

navigator.geolocation.getCurrentPosition(success => {
alert("Good to go");
}, failure => {
if (failure.message.startsWith("Only secure origins are allowed")) {
alert("Failed, Secure login issue");

As updated policies over web componants by google, Geolocation can only be used with secure origins i.e, using HTTPS connection.(Strongly recommended) Although i am too working to find a alternate way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100