1

I am getting the user's location using the HTML5 Geolocation api like this:

navigator.geolocation.getCurrentPosition(function (position) {
        loadWeather(position.coords.latitude + ',' + position.coords.longitude); //load weather using your lat/lng coordinates
  });

jQuery(document).ready(function() {
        loadWeather('Seattle',''); //@params location, woeid
});

function loadWeather(location, woeid) {
    jQuery.simpleWeather({
        location: location,
        woeid: woeid,
        unit: 'c',
        success: function(weather) {
        html = '<div class="weather_widget text-right"><h2><div class="icon_weather"><i class="icon-'+weather.code+'"></i></div><div class="degrees"><div class="temp_weather">'+weather.temp+'</div><div class="weather_deg">&deg;</div><div class="unit_weather">'+weather.units.temp+'</div></div></h2>';
        html += '<div class="city_weather">'+weather.city+'</div></div>';

       jQuery("#weather").html(html);
   },
   error: function(error) {
       jQuery("#weather").html('<p>'+error+'</p>');
  }
  });
}

This basically loads a widget that shows the weather based on the geolocation position.

Now my questions are:

  1. How could I store the location variable for the entire session? (So that it does not ask for the location every time a new page is loaded?)

  2. How can I make the location variable accessible to the entire website, so that I can use that location in other functionality on the website? (Such as a google map).

The geolocation code is located in a header file that is included across the site.

user990423
  • 1,397
  • 2
  • 12
  • 32
Jacques
  • 1,649
  • 2
  • 14
  • 23
  • 3
    You could store the location client side through cookies or sessionStorage. Both accessible with JavaScript. – musse1 Dec 08 '15 at 15:31
  • @SergeyBelyakov - why do you recommend localStorage over sessionStorage? Is it a matter of preference or is there a specific reason? – Jacques Dec 08 '15 at 15:51
  • @musse1 Thank you for your expedited assistance. – Jacques Dec 08 '15 at 15:51
  • @Jacques , sessionStorage (as the name persists) is only available for the duration of the browser session (and is deleted when the window is closed) more there http://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookies – Sergey Belyakov Dec 08 '15 at 21:10

1 Answers1

4

Hi you need to use localStorage or sessionStorage of javaScript

To store data in localstorage use below code

localStorage.setItem('veriable_name','value');

To get value use below code

localStorage.getItem('veriable_name');

To clear localStorage use

localStorage.clear();

more info on Local Storage and session Storage

Amin Kodaganur
  • 646
  • 6
  • 19