Top Questions

  • 2.9 Years

    • Category : Jquery
    • Location : Chandigarh
    • Posted On : Jul 5, 2017

    Get geo location using jquery and save in a cookie

    var jb_lat = getCookie('jb_lat');
    var jb_lng = getCookie('jb_lng');
    
    if(jb_lat == '' && jb_lng == ''){
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else { 
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
        function showPosition(location){
            var exdays = 10;
            var d = new Date();
            d.setTime(d.getTime()   (exdays*24*60*60*1000));
            var expires = "expires="  d.toUTCString();
            document.cookie = "jb_lat="   location.coords.latitude   ";"   expires   ";path=/";
            document.cookie = "jb_lng="   location.coords.longitude   ";"   expires   ";path=/";
            window.location.reload();
        }
    
    }
        function getCookie(cname) {
            var name = cname   "=";
            var decodedCookie = decodeURIComponent(document.cookie);
            var ca = decodedCookie.split(';');
            for(var i = 0; i <ca.length; i  ) {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                    c = c.substring(1);
                }
                if (c.indexOf(name) == 0) {
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        }

Add Your Answer

When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.