Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从ID创建Cookie?_Javascript_Cookies_Location_Geo - Fatal编程技术网

Javascript 从ID创建Cookie?

Javascript 从ID创建Cookie?,javascript,cookies,location,geo,Javascript,Cookies,Location,Geo,我发现了这个很棒的脚本,它可以根据用户的经度和纬度来查找他们的邮政编码。通过阅读代码,我发现必须有一个状态id才能使信息可见。我的问题是,是否有可能将信息制作成cookie放入状态id中。因此,它会根据个人输入一个邮政编码,我想制作一个cookie,用于在我的网站上进一步使用该邮政编码。它使用地理定位,将信息插入谷歌,并获得邮政编码。因此,如果可能的话,我想捕获该邮政编码并从中创建一个cookie。任何帮助都将不胜感激。谢谢 <script src="http://ajax.googlea

我发现了这个很棒的脚本,它可以根据用户的经度和纬度来查找他们的邮政编码。通过阅读代码,我发现必须有一个状态id才能使信息可见。我的问题是,是否有可能将信息制作成cookie放入状态id中。因此,它会根据个人输入一个邮政编码,我想制作一个cookie,用于在我的网站上进一步使用该邮政编码。它使用地理定位,将信息插入谷歌,并获得邮政编码。因此,如果可能的话,我想捕获该邮政编码并从中创建一个cookie。任何帮助都将不胜感激。谢谢

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"    type="text/javascript" charset="utf-8"></script>
</head>

 <body>
 <p>&nbsp;</p>
<center>
<p id="status"></p>
<script>
$(function(){
var GETZIP = {
  getLocation: function(){
     $('#status').text('Searching...');
     if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(GETZIP.getZipCode, GETZIP.error,     {timeout: 7000});//cache it for 10 minutes
     }else{
        GETZIP.error('Geo location not supported');
     }
  },
  index: 0,
  error: function(msg) {
     if(msg.code){
        //this is a geolocation error
        switch(msg.code){
        case 1:
           $("#status").text('Permission Denied').fadeOut().fadeIn();
           break;
        case 2:
           $("#status").text('Position Unavailable').fadeOut().fadeIn();
           break;
        case 3:
           GETZIP.index++;
           $("#status").text('Timeout... Trying again (' + GETZIP.index +  ')').fadeOut().fadeIn();
           navigator.geolocation.getCurrentPosition(GETZIP.getZipCode, GETZIP.error,  {timeout: 7000});
           break;
        default:
           //nothing
        }
     }else{
        //this is a text error
        $('#error').text(msg).addClass('failed');
     }

  },

  getZipCode: function(position){
     var position = position.coords.latitude + "," + position.coords.longitude;
     $.getJSON('proxy.php',{
        path : "http://maps.google.com/maps/api/geocode/json?latlng="+position+"&sensor=false",
        type: "application/json"
     }, function(json){
        //Find the zip code of the first result
        if(!(json.status == "OK")){
           GETZIP.error('Zip Code not Found');
           return;
        }
        var found = false;
        $(json.results[0].address_components).each(function(i, el){
           if($.inArray("postal_code", el.types) > -1){
              $("#status").text(Your zip code: + el.short_name);
              found = true;
              return;
           }
        });
        if(!found){
           GETZIP.error('Zip Code not Found');
        }
     });
  }
}
GETZIP.getLocation();
});
</script>

$(函数(){ var GETZIP={ getLocation:function(){ $('#status').text('搜索…'); if(导航器.地理位置){ navigator.geolocation.getCurrentPosition(GETZIP.getZipCode,GETZIP.error,{timeout:7000});//缓存10分钟 }否则{ GETZIP.error('不支持地理位置'); } }, 索引:0, 错误:函数(msg){ 如果(消息代码){ //这是一个地理位置错误 开关(消息代码){ 案例1: $(“#状态”).text('Permission Denied').fadeOut().fadeIn(); 打破 案例2: $(“#状态”).text('Position Unavailable').fadeOut().fadeIn(); 打破 案例3: GETZIP.index++; $(“#status”).text('超时…重试('+GETZIP.index+')).fadeOut().fadeIn(); navigator.geolocation.getCurrentPosition(GETZIP.getZipCode,GETZIP.error,{timeout:7000}); 打破 违约: //没什么 } }否则{ //这是一个文本错误 $('#error').text(msg.addClass('failed'); } }, getZipCode:函数(位置){ var position=position.coords.latitude+“,”+position.coords.longitude; $.getJSON('proxy.php'{ 路径:“http://maps.google.com/maps/api/geocode/json?latlng=“+位置+”&传感器=假”, 类型:“application/json” },函数(json){ //查找第一个结果的邮政编码 如果(!(json.status==“OK”)){ GETZIP.error('未找到邮政编码'); 返回; } var=false; $(json.results[0]。地址\组件)。每个(函数(i,el){ 如果($.inArray(“邮政编码”,el.types)>-1){ $(“#status”).text(您的邮政编码:+el.short_name); 发现=真; 返回; } }); 如果(!找到){ GETZIP.error('未找到邮政编码'); } }); } } GETZIP.getLocation(); });
假设您的代码在
el.short\u name
中正确检索了邮政编码,您只需在检索后立即存储该值:

以下是创建cookie的函数:

function createCookie(name,value,days) {
    var expires = "", date;
    if (days) {
        date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name+"="+value+expires+"; path=/";
}
下面是getZipCode的修改版本,它在检索邮政编码后立即写入cookie:

  getZipCode: function(position){
     var position = position.coords.latitude + "," + position.coords.longitude;
     $.getJSON('proxy.php',{
        path : "http://maps.google.com/maps/api/geocode/json?latlng="+position+"&sensor=false",
        type: "application/json"
     }, function(json){
        //Find the zip code of the first result
        if(!(json.status == "OK")){
           GETZIP.error('Zip Code not Found');
           return;
        }
        var found = false;
        $(json.results[0].address_components).each(function(i, el){
           if($.inArray("postal_code", el.types) > -1){
              $("#status").text(Your zip code: + el.short_name);
              createCookie("zipcode", el.short_name, 365);
              found = true;
              return;
           }
        });
        if(!found){
           GETZIP.error('Zip Code not Found');
        }
     });
  }

您的问题只是“如何在cookie中存储zipcode?”。或者还有其他的吗?没有。这是如何捕获邮政编码并将其存储在cookie中的问题。邮政编码似乎被定义为“el.short\u name”,我无法找到一种方法将其正确存储为cookie。为什么您不能将
el.short\u name
放入cookie中?你试了什么?你有什么问题?我试过“el.shortname”的任何东西。我尝试将id状态转换为表单值,这样我就可以轻松地从表单值中创建一个cookie。但是我没有那么幸运。只需在这一行
$(“#状态”).text(您的邮政编码:+el.short_name)之后创建一个带有
el.short_name
的cookie