Question

I have below mentioned JSFidle.But it's not working.Can you say why's that ?

Note: I want to run it on JSFiddle.It should show the Latitude and Longitude.

JSFiddle Geolocation

var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;  
}
Was it helpful?

Solution

Because you have your JavaScript set to run in the load event (the second drop down on the left). The function getLocation only exists within the scope of that load handler, not in the global scope you're trying to call it from.

It works fine if you change it to 'No wrap - in <body>'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top