简体   繁体   中英

Jquery Cookie not being read correctly

I am trying to set a cookie, and then read it and put it into my google geocoding script but it doesnt seem to work. It should be so the only autosuggestion results are from the US but you can see in the example that they are being suggested from every country.

This is the code I am working with:

http://jsfiddle.net/sR4GR/4/

This is the raw code:

<script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=true&libraries=places"></script>
<script type="text/javascript" src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>

<script type="text/javascript">

    // SET COOKIE FOR TESTING   
$.cookie("country", "us");

    // GEOCODE RESULT
 function geocode(){
    var GeoCoded = { done: false };
    var input = document.getElementById('loc');
    var options = { types: []};
    var country_code = $.cookie('country');
    if (country_code) { options.componentRestrictions= { 'country': country_code }; }
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    $('#searchform').on('submit',function(e){
       if(GeoCoded.done)
            return true;
        e.preventDefault();
        var geocoder = new google.maps.Geocoder();
        var address = document.getElementById('loc').value;

        $('#searchform input[type="submit"]').attr('disabled',true);

        geocoder.geocode({
            'address': address
        },
        function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                $('#lat').val(results[0].geometry.location.lat());
                $('#lng').val(results[0].geometry.location.lng());
                GeoCoded.done = true;
                $('#searchform').submit();
            } else {
                $('#searchform input[type="submit"]').attr('disabled',false);
                alert("Damn! We couldn't find this location")
            }

        });

    });

};      
</script>

<body onload="geocode()">
<form name="searchform">
        <input class="kw" id="keyword" placeholder="Keyword"></input>
        <input id="loc" placeholder="Location" type="text"></input>
        <input type="submit" value="Search" id="search">
        <input class="hidden" id="lat" disabled="true" placeholder="lat"></input>
        <input class="hidden" id="lng" disabled="true" placeholder="lng"></input>
</form>

The above code is written and executed correctly. The cookie is set in Chrome and Firefox. In Safari, the default cookie setting is "From visited," which is a relatively strict setting. To allow these cookies to be set, the user would have to change the Safari settings to allow cookies "Always."

While there doesn't seem to be a classy workaround for this, one possible resolution would be to check if the cookie returns undefined and if so, alert the user to "Please go to settings and turn on cookies so my awesome site can run effectively on your security-conscious device. Mmkay? Thanks."

This thread contains more information on Safari and cookies .

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