0

I am using this plugin to change convert an object to a string in JavaScript. I want to convert the geolocation object to a string.

My example below gives me the correct results in Chrome, but not in Firefox. Here is a demo.

Plugin:

jQuery.extend({
    stringify : function stringify(obj) {
        alert('Hiii');
        var t = typeof (obj);
        if (t != "object" || obj === null) {
            // simple data type
            if (t == "string") 
                obj = '"' + obj + '"';
            return String(obj);
        } else {
            // recurse array or object
            var n, v, json = [], arr = (obj && obj.constructor == Array);
            for (n in obj) {
                v = obj[n];
                t = typeof(v);
                if (obj.hasOwnProperty(n)) {
                    if (t == "string") 
                        v = '"' + v + '"';
                    else if (t == "object" && v !== null)
                        v = jQuery.stringify(v);
                    json.push((arr ? "" : '"' + n + '":') + String(v));
                }
            }
            return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
        }
    }
});

Code

<script>
$(function(){
    function getObject(){
        if (navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(function(position) {
                alert(position);
                alert(jQuery.stringify(position));
            }, function(error)
            {
                alert("Error occurred. Error code: " + error.code);
            });
        }
    }
    $('#getObj').on('click',function(){
        getObject();
    });
});
</script>
<div>
    <a data-role="button" id="getObj">Get Position Object</a>
</div>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Blu
  • 4,036
  • 6
  • 38
  • 65
  • its very important to tell why pull down the vote Because i checked different question on `stack overflow` and fellow them but in my case all has same reslut – Blu Feb 19 '14 at 08:35
  • 1
    Is there a reason you use this custom made function instead of `JSON.stringify`? Also, what exactly do you mean by *"but same example in Firefox not working"*. *How* does it not work? What happens and what do you expect to happen? – Felix Kling Feb 19 '14 at 08:38
  • 1
    In Firefox it displays the empty string I want to change in String and store object in `local Storage` I also tried that function `JSON.stringify` in firefox the result is `{}` and in Chrome `{"timestamp":"121"..... // A full string text` @FelixKling – Blu Feb 19 '14 at 08:42
  • As @FelixKling suggested, why don't you use 'JSON.stringfy' to convert your object to string, it works every where with no problem, and to convert json string back to object you can easly use 'JSON.parse' . – Dabbas Feb 19 '14 at 08:44
  • Sorry @FelixKling, seems we were both editing at the same time. – Jamie Taylor Feb 19 '14 at 08:45
  • Can you show us you test in jsFiddle example ? because JSON.stringfy is standard and works every where – Dabbas Feb 19 '14 at 08:45
  • I put one :) but that one Edited and removed anyways here is jsfiddle link http://jsfiddle.net/TLE33/6/ – Blu Feb 19 '14 at 08:46
  • possible duplicate of [FF 13, IE 9: JSON stringify / geolocation object](http://stackoverflow.com/questions/11042212/ff-13-ie-9-json-stringify-geolocation-object) – Felix Kling Feb 19 '14 at 08:49
  • @BluAngel: No one removed the link to your fiddle. It's still in the question. – Felix Kling Feb 19 '14 at 08:49
  • ok @FelixKling but mt FF is 27 – Blu Feb 19 '14 at 08:52
  • The behavior didn't change. – Felix Kling Feb 19 '14 at 08:52
  • So its not possible that we stringify the object in FF? – Blu Feb 19 '14 at 08:54

0 Answers0