1

I got the HF HTML5 book , and when i reached to the chapter about geolocation and typed the code in , it doesn't work , i tried enabling the geo-location features in preferences but still no change , any suggestions?

The JS:

window.onload = getMyLocation;
    function getMyLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(displayLocation);
        } else {
            alert("Oops, no geolocation support");
    }
};

function displayLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var div = document.getElementById("location");
    div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
};

Problem was solved.

aleksXPO
  • 123
  • 1
  • 10

2 Answers2

2

I found out that it's a bug from chrome , every file:// has it , you can't do it without a server like WAMP , or you can use an other browser.

aleksXPO
  • 123
  • 1
  • 10
-1

Try binding event handler like this:

window.onload = new function() { getMyLocation() };

Here is the fiddle.

OzrenTkalcecKrznaric
  • 5,535
  • 4
  • 34
  • 57
  • Wrong. This would call the event listener *before* the event occurred, on assignment. There is no syntax error, but the indentation is off. – PointedEars Jun 30 '13 at 11:54
  • That's not an error , and i found out that it's because you have to use a server or other browser , the file:// if bugged in chrome. – aleksXPO Jun 30 '13 at 11:55
  • @PointedEars: thanks, I was clumsy. Answer updated from my existing project :) – OzrenTkalcecKrznaric Jun 30 '13 at 11:58
  • @OzrenTkalčecKrznarić Your new code does not work. It would return a reference to a new, *non-callable* object and assign that to `window.onload`. An event listener must be *callable*. Also, it is still executing `getMyLocation()` before the event occurs, because you are calling the constructor *on assignment* already. – PointedEars Jun 30 '13 at 12:00
  • @aleksXPO You can either edit your question, hoping for a solution with `file://`, or post an answer to your own question. – PointedEars Jun 30 '13 at 12:01
  • @PointedEars: fiddle works fine. Question doesn't imply local file reference. – OzrenTkalcecKrznaric Jun 30 '13 at 12:02
  • @OzrenTkalčecKrznarić Please read my updated comment and read a decent tutorial before you make further suggestions. – PointedEars Jun 30 '13 at 12:03
  • @PointedEars: It's a shame I have to repeat but it's still not in the question. The code is unobtrusive and has error in itself but semantics at least looks fine and code executes. For 'nice' onload binding see [http://stackoverflow.com/questions/559150/best-practice-for-using-window-onload](http://stackoverflow.com/questions/559150/best-practice-for-using-window-onload). – OzrenTkalcecKrznaric Jun 30 '13 at 12:14
  • @OzrenTkalčecKrznarić Both your RHS value and your timing are wrong. First the timing, then the value. – PointedEars Jun 30 '13 at 12:16
  • @PointedEars: Chrome/IE/FF shows no error in the fiddle. There is a standard and there is a real world. I repeat, my code is not 100% kosher but it works. – OzrenTkalcecKrznaric Jun 30 '13 at 12:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32635/discussion-between-ozren-tkalcec-krznaric-and-pointedears) – OzrenTkalcecKrznaric Jun 30 '13 at 12:25