简体   繁体   中英

javascript cookie value variable

The cookies should save the longitude and latitude values from the function, but my browser (chrome) show that the value is undefinded. How can I fix it that the longitude and latutide coordinates become the values of the cookies? Other asked how can I add a variable at a cookie?

    <html>
<head>
    <meta charset="UTF-8" /> 
    <title>
        HTML Document Structure
    </title>
    <link rel="stylesheet" type="text/css" href="style.css" />

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>

    <!-- Einstellungen zur Defintion als WebApp -->
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

</head>
<body>

<div id="wrapper">

    <a href="next.html" onclick="geoFindMe()" data-role="button">new chat</a>

</div>
<div class="gradient"></div>

<script type="text/javascript">


var currentdate = new Date(); 
var datetime = "Last Sync: " + currentdate.getDate() + 
                + (currentdate.getMonth()+1)  +  
                + currentdate.getFullYear() +   
                + (currentdate.getHours()+24) +   
                + currentdate.getMinutes() +  
                + currentdate.getSeconds();



function geoFindMe() {
  if (!navigator.geolocation){
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;

    document.cookie = 'latitude='+latitude+'; '+datetime+'';  
document.cookie = 'longitude='+longitude+'; '+datetime+'';
  };


  function error() {
    output.innerHTML = "Unable to retrieve your location";
  };

  output.innerHTML = "<p>Locating…</p>";

  navigator.geolocation.getCurrentPosition(success, error);
}
</script>
</body>
</html>

latitude and longitude are undefined at the time you save the cookie...

Save the cookie in your success function, not on page load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM