javascript cookie值变量

时间:2015-03-22 19:40:46

标签: javascript html cookies

Cookie应保存函数的经度和纬度值,但我的浏览器(chrome)显示该值未定。如何修复经度和latutide坐标成为cookie的值?其他问我如何在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>

1 个答案:

答案 0 :(得分:0)

保存Cookie时

latitudelongitude未定义...

将Cookie保存在您的成功函数中,而不是在页面加载中。