How can get a variable value when I leave my page

How can get a variable value when I leave my page

Dear All,

I am loosing my time looking to get a value when I leave a page.
On my home page I have a code which record my position in a data called tracking_data

  1. $('#home').live('pagebeforeshow', function(e){
       
        $('#mapHome').css('height', Resize.content()-35 +'px');
         // Start tracking the User
        var track_id = "me";
       
        watch_id = navigator.geolocation.watchPosition(
            // Success
            function(position){
                tracking_data.push(position);
                //alert(tracking_data[0].coords.latitude);
           
            },
            // Error
            function(error){
                console.log(error);
            },
            // Settings
            { frequency: 3000, enableHighAccuracy: false
        });


















The GPS position are well recorded in tracking_data variable.
But when I leave my page I would like to record the tracking_data in localStorage.

For that I did it:
  1. $('#home').live("pagebeforehide", function() {
        alert(tracking_data[0].coords.latitude);
        alert('track_id : ' + track_id);
        alert('Bye bye');
        // Stop tracking the user
       
        navigator.geolocation.clearWatch(watch_id);
       
        // Save the tracking data
        window.localStorage.setItem(track_id, JSON.stringify(tracking_data));
       
        console.log(localStorage);

        // Reset watch_id and tracking_data
        //var watch_id = null;
        //var tracking_data = null;
       
    });
















The above problem.
The first alert, display the latitude
The second alert does not display "me"
The tracking_data in the JSON varaible is empty

Is there a way to get the tracking_data create in pagebeforeshow function, in order to record it?

Many thank for your help and happy christmas
Cheers