Question about navigator.geolocation

I’m using navigator for the weather app assignment, to get started I used the code I learned from the previous exercise. Here’s the code snippet of my window load function so far:

$(window).load(function ( {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            $("#data").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
            lat = position.coords.latitude;
            lon = position.coords.longitude;
        });
    }
});

Lat is a variable for latitude, lon is a variable of longitude, and the $("#data") section will be removed and replaced with weather app related code. Anyway, the problem is: if (navigator.geolocation) {, the error is: Uncaught SyntaxError: Unexpected token .

Any help is appreciated.

The correct syntax for a function with no parameters is function() {. You are missing a closing parentheses in your first line of code.

Also, instead of using:

$(window).load(

try using:

$(document).ready(
1 Like

OK. Quick question (I know they covered this in the lessons but if you know this will be quicker): When would you use window instead of document?

@glennqhurd,
you can see the explanation here:

3 Likes
$(document).ready(function() {


});

This will run your code when the page first loads.