FF 13, IE 9: JSON stringify / geolocation object
I'm trying to get Firefox 13 to turn a geolocation position object into a JSON string, but it's returning an empty string rather than the correct string representation of my JSON object. This is working fine in the latest versions of Chrome and Safari, as well as the Android browser. Here's my code:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
//Success handler
console.log(position); //This outputs the position object to the console
var gps = JSON.stringify(position);
console.log(gps); //This outputs an empty string!
},
function (error)
{
//Handle error
},
{ maximumAge: 3000, timeout: 60000, enableHighAccuracy: true }
);
}
else {
//Handle error
}
In Chrome, this outputs a geolocation object, and this string:
"{"coords":{"latitude":XYZ,"heading":null,"accuracy":40,"altitudeAccuracy":null,"altitude":null,"longitude":XYZ,"speed":null},"timestamp":1339712284200}"
However, in Firefox 13 the output is just an empty string, even though the geolocation object that's printed to the console is to all intents and purposes the same as the object displayed by Chrome. Any ideas on what's going wrong here? This seems to be a related issue, but I don't see a solution there either. IE9 displays the same behaviour, by the way.
javascript json firefox geolocation internet-explorer-9
add a comment |
I'm trying to get Firefox 13 to turn a geolocation position object into a JSON string, but it's returning an empty string rather than the correct string representation of my JSON object. This is working fine in the latest versions of Chrome and Safari, as well as the Android browser. Here's my code:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
//Success handler
console.log(position); //This outputs the position object to the console
var gps = JSON.stringify(position);
console.log(gps); //This outputs an empty string!
},
function (error)
{
//Handle error
},
{ maximumAge: 3000, timeout: 60000, enableHighAccuracy: true }
);
}
else {
//Handle error
}
In Chrome, this outputs a geolocation object, and this string:
"{"coords":{"latitude":XYZ,"heading":null,"accuracy":40,"altitudeAccuracy":null,"altitude":null,"longitude":XYZ,"speed":null},"timestamp":1339712284200}"
However, in Firefox 13 the output is just an empty string, even though the geolocation object that's printed to the console is to all intents and purposes the same as the object displayed by Chrome. Any ideas on what's going wrong here? This seems to be a related issue, but I don't see a solution there either. IE9 displays the same behaviour, by the way.
javascript json firefox geolocation internet-explorer-9
add a comment |
I'm trying to get Firefox 13 to turn a geolocation position object into a JSON string, but it's returning an empty string rather than the correct string representation of my JSON object. This is working fine in the latest versions of Chrome and Safari, as well as the Android browser. Here's my code:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
//Success handler
console.log(position); //This outputs the position object to the console
var gps = JSON.stringify(position);
console.log(gps); //This outputs an empty string!
},
function (error)
{
//Handle error
},
{ maximumAge: 3000, timeout: 60000, enableHighAccuracy: true }
);
}
else {
//Handle error
}
In Chrome, this outputs a geolocation object, and this string:
"{"coords":{"latitude":XYZ,"heading":null,"accuracy":40,"altitudeAccuracy":null,"altitude":null,"longitude":XYZ,"speed":null},"timestamp":1339712284200}"
However, in Firefox 13 the output is just an empty string, even though the geolocation object that's printed to the console is to all intents and purposes the same as the object displayed by Chrome. Any ideas on what's going wrong here? This seems to be a related issue, but I don't see a solution there either. IE9 displays the same behaviour, by the way.
javascript json firefox geolocation internet-explorer-9
I'm trying to get Firefox 13 to turn a geolocation position object into a JSON string, but it's returning an empty string rather than the correct string representation of my JSON object. This is working fine in the latest versions of Chrome and Safari, as well as the Android browser. Here's my code:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
//Success handler
console.log(position); //This outputs the position object to the console
var gps = JSON.stringify(position);
console.log(gps); //This outputs an empty string!
},
function (error)
{
//Handle error
},
{ maximumAge: 3000, timeout: 60000, enableHighAccuracy: true }
);
}
else {
//Handle error
}
In Chrome, this outputs a geolocation object, and this string:
"{"coords":{"latitude":XYZ,"heading":null,"accuracy":40,"altitudeAccuracy":null,"altitude":null,"longitude":XYZ,"speed":null},"timestamp":1339712284200}"
However, in Firefox 13 the output is just an empty string, even though the geolocation object that's printed to the console is to all intents and purposes the same as the object displayed by Chrome. Any ideas on what's going wrong here? This seems to be a related issue, but I don't see a solution there either. IE9 displays the same behaviour, by the way.
javascript json firefox geolocation internet-explorer-9
javascript json firefox geolocation internet-explorer-9
edited Jun 14 '12 at 22:32
asked Jun 14 '12 at 22:26
Daan
2,9951719
2,9951719
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
What's going on is that JSON.stringify only looks at the object's own properties by default.
And per DOM specs all DOM properties actually live on the object's prototype.
IE and Firefox implement the spec correctly by putting the properties on the prototype. Chrome and Safari do not: they put the properties directly on the object. That makes this case work, but breaks other things (e.g. the ability to hook the property getters and setters)....
There's talk of adding toJSON methods to some DOM objects to give them more reasonable behavior for JSON.stringify.
3
geolocation has nothing to do with DOM.
– user123444555621
Jun 15 '12 at 5:38
Thanks! I did figure out a workaround is simply to assign the properties to a new variable and stringify that, but it was unclear to me why that worked while my earlier code didn't, which I didn't like. Now I understand.
– Daan
Jun 15 '12 at 7:31
1
@Pumbaa80 More precisely, the WebIDL specification defines the behavior here. But feel free to nitpick as desired!
– Boris Zbarsky
Jun 15 '12 at 9:24
@Daan Did you solved your problem?
– Blu
Feb 19 '14 at 10:35
2
Chrome has since implemented this "correctly" and now its Positions and Coordinates don't stringify anymore either.
– iabw
Mar 30 '16 at 13:39
add a comment |
I created a clone function to clone the Geolocation position (or any other) object into an object that will be stringified as expected:
function cloneAsObject(obj) {
if (obj === null || !(obj instanceof Object)) {
return obj;
}
var temp = (obj instanceof Array) ? : {};
// ReSharper disable once MissingHasOwnPropertyInForeach
for (var key in obj) {
temp[key] = cloneAsObject(obj[key]);
}
return temp;
}
Note: May not support types not used in Geoposition type (eg Date)
You would then use it as follows in your code:
var gps = JSON.stringify(cloneAsObject(position));
Hope this helps someone :)
I wish this was the accepted answer. I passed this up initially, then 20 minutes later found a blog post that pointed back here. Could have saved me some time.
– Scott Beeson
Oct 4 '17 at 16:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11042212%2fff-13-ie-9-json-stringify-geolocation-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What's going on is that JSON.stringify only looks at the object's own properties by default.
And per DOM specs all DOM properties actually live on the object's prototype.
IE and Firefox implement the spec correctly by putting the properties on the prototype. Chrome and Safari do not: they put the properties directly on the object. That makes this case work, but breaks other things (e.g. the ability to hook the property getters and setters)....
There's talk of adding toJSON methods to some DOM objects to give them more reasonable behavior for JSON.stringify.
3
geolocation has nothing to do with DOM.
– user123444555621
Jun 15 '12 at 5:38
Thanks! I did figure out a workaround is simply to assign the properties to a new variable and stringify that, but it was unclear to me why that worked while my earlier code didn't, which I didn't like. Now I understand.
– Daan
Jun 15 '12 at 7:31
1
@Pumbaa80 More precisely, the WebIDL specification defines the behavior here. But feel free to nitpick as desired!
– Boris Zbarsky
Jun 15 '12 at 9:24
@Daan Did you solved your problem?
– Blu
Feb 19 '14 at 10:35
2
Chrome has since implemented this "correctly" and now its Positions and Coordinates don't stringify anymore either.
– iabw
Mar 30 '16 at 13:39
add a comment |
What's going on is that JSON.stringify only looks at the object's own properties by default.
And per DOM specs all DOM properties actually live on the object's prototype.
IE and Firefox implement the spec correctly by putting the properties on the prototype. Chrome and Safari do not: they put the properties directly on the object. That makes this case work, but breaks other things (e.g. the ability to hook the property getters and setters)....
There's talk of adding toJSON methods to some DOM objects to give them more reasonable behavior for JSON.stringify.
3
geolocation has nothing to do with DOM.
– user123444555621
Jun 15 '12 at 5:38
Thanks! I did figure out a workaround is simply to assign the properties to a new variable and stringify that, but it was unclear to me why that worked while my earlier code didn't, which I didn't like. Now I understand.
– Daan
Jun 15 '12 at 7:31
1
@Pumbaa80 More precisely, the WebIDL specification defines the behavior here. But feel free to nitpick as desired!
– Boris Zbarsky
Jun 15 '12 at 9:24
@Daan Did you solved your problem?
– Blu
Feb 19 '14 at 10:35
2
Chrome has since implemented this "correctly" and now its Positions and Coordinates don't stringify anymore either.
– iabw
Mar 30 '16 at 13:39
add a comment |
What's going on is that JSON.stringify only looks at the object's own properties by default.
And per DOM specs all DOM properties actually live on the object's prototype.
IE and Firefox implement the spec correctly by putting the properties on the prototype. Chrome and Safari do not: they put the properties directly on the object. That makes this case work, but breaks other things (e.g. the ability to hook the property getters and setters)....
There's talk of adding toJSON methods to some DOM objects to give them more reasonable behavior for JSON.stringify.
What's going on is that JSON.stringify only looks at the object's own properties by default.
And per DOM specs all DOM properties actually live on the object's prototype.
IE and Firefox implement the spec correctly by putting the properties on the prototype. Chrome and Safari do not: they put the properties directly on the object. That makes this case work, but breaks other things (e.g. the ability to hook the property getters and setters)....
There's talk of adding toJSON methods to some DOM objects to give them more reasonable behavior for JSON.stringify.
answered Jun 15 '12 at 5:17
Boris Zbarsky
31.6k33948
31.6k33948
3
geolocation has nothing to do with DOM.
– user123444555621
Jun 15 '12 at 5:38
Thanks! I did figure out a workaround is simply to assign the properties to a new variable and stringify that, but it was unclear to me why that worked while my earlier code didn't, which I didn't like. Now I understand.
– Daan
Jun 15 '12 at 7:31
1
@Pumbaa80 More precisely, the WebIDL specification defines the behavior here. But feel free to nitpick as desired!
– Boris Zbarsky
Jun 15 '12 at 9:24
@Daan Did you solved your problem?
– Blu
Feb 19 '14 at 10:35
2
Chrome has since implemented this "correctly" and now its Positions and Coordinates don't stringify anymore either.
– iabw
Mar 30 '16 at 13:39
add a comment |
3
geolocation has nothing to do with DOM.
– user123444555621
Jun 15 '12 at 5:38
Thanks! I did figure out a workaround is simply to assign the properties to a new variable and stringify that, but it was unclear to me why that worked while my earlier code didn't, which I didn't like. Now I understand.
– Daan
Jun 15 '12 at 7:31
1
@Pumbaa80 More precisely, the WebIDL specification defines the behavior here. But feel free to nitpick as desired!
– Boris Zbarsky
Jun 15 '12 at 9:24
@Daan Did you solved your problem?
– Blu
Feb 19 '14 at 10:35
2
Chrome has since implemented this "correctly" and now its Positions and Coordinates don't stringify anymore either.
– iabw
Mar 30 '16 at 13:39
3
3
geolocation has nothing to do with DOM.
– user123444555621
Jun 15 '12 at 5:38
geolocation has nothing to do with DOM.
– user123444555621
Jun 15 '12 at 5:38
Thanks! I did figure out a workaround is simply to assign the properties to a new variable and stringify that, but it was unclear to me why that worked while my earlier code didn't, which I didn't like. Now I understand.
– Daan
Jun 15 '12 at 7:31
Thanks! I did figure out a workaround is simply to assign the properties to a new variable and stringify that, but it was unclear to me why that worked while my earlier code didn't, which I didn't like. Now I understand.
– Daan
Jun 15 '12 at 7:31
1
1
@Pumbaa80 More precisely, the WebIDL specification defines the behavior here. But feel free to nitpick as desired!
– Boris Zbarsky
Jun 15 '12 at 9:24
@Pumbaa80 More precisely, the WebIDL specification defines the behavior here. But feel free to nitpick as desired!
– Boris Zbarsky
Jun 15 '12 at 9:24
@Daan Did you solved your problem?
– Blu
Feb 19 '14 at 10:35
@Daan Did you solved your problem?
– Blu
Feb 19 '14 at 10:35
2
2
Chrome has since implemented this "correctly" and now its Positions and Coordinates don't stringify anymore either.
– iabw
Mar 30 '16 at 13:39
Chrome has since implemented this "correctly" and now its Positions and Coordinates don't stringify anymore either.
– iabw
Mar 30 '16 at 13:39
add a comment |
I created a clone function to clone the Geolocation position (or any other) object into an object that will be stringified as expected:
function cloneAsObject(obj) {
if (obj === null || !(obj instanceof Object)) {
return obj;
}
var temp = (obj instanceof Array) ? : {};
// ReSharper disable once MissingHasOwnPropertyInForeach
for (var key in obj) {
temp[key] = cloneAsObject(obj[key]);
}
return temp;
}
Note: May not support types not used in Geoposition type (eg Date)
You would then use it as follows in your code:
var gps = JSON.stringify(cloneAsObject(position));
Hope this helps someone :)
I wish this was the accepted answer. I passed this up initially, then 20 minutes later found a blog post that pointed back here. Could have saved me some time.
– Scott Beeson
Oct 4 '17 at 16:46
add a comment |
I created a clone function to clone the Geolocation position (or any other) object into an object that will be stringified as expected:
function cloneAsObject(obj) {
if (obj === null || !(obj instanceof Object)) {
return obj;
}
var temp = (obj instanceof Array) ? : {};
// ReSharper disable once MissingHasOwnPropertyInForeach
for (var key in obj) {
temp[key] = cloneAsObject(obj[key]);
}
return temp;
}
Note: May not support types not used in Geoposition type (eg Date)
You would then use it as follows in your code:
var gps = JSON.stringify(cloneAsObject(position));
Hope this helps someone :)
I wish this was the accepted answer. I passed this up initially, then 20 minutes later found a blog post that pointed back here. Could have saved me some time.
– Scott Beeson
Oct 4 '17 at 16:46
add a comment |
I created a clone function to clone the Geolocation position (or any other) object into an object that will be stringified as expected:
function cloneAsObject(obj) {
if (obj === null || !(obj instanceof Object)) {
return obj;
}
var temp = (obj instanceof Array) ? : {};
// ReSharper disable once MissingHasOwnPropertyInForeach
for (var key in obj) {
temp[key] = cloneAsObject(obj[key]);
}
return temp;
}
Note: May not support types not used in Geoposition type (eg Date)
You would then use it as follows in your code:
var gps = JSON.stringify(cloneAsObject(position));
Hope this helps someone :)
I created a clone function to clone the Geolocation position (or any other) object into an object that will be stringified as expected:
function cloneAsObject(obj) {
if (obj === null || !(obj instanceof Object)) {
return obj;
}
var temp = (obj instanceof Array) ? : {};
// ReSharper disable once MissingHasOwnPropertyInForeach
for (var key in obj) {
temp[key] = cloneAsObject(obj[key]);
}
return temp;
}
Note: May not support types not used in Geoposition type (eg Date)
You would then use it as follows in your code:
var gps = JSON.stringify(cloneAsObject(position));
Hope this helps someone :)
edited Jun 9 '16 at 13:21
answered Jun 9 '16 at 12:56
Mark Whitfeld
2,26512225
2,26512225
I wish this was the accepted answer. I passed this up initially, then 20 minutes later found a blog post that pointed back here. Could have saved me some time.
– Scott Beeson
Oct 4 '17 at 16:46
add a comment |
I wish this was the accepted answer. I passed this up initially, then 20 minutes later found a blog post that pointed back here. Could have saved me some time.
– Scott Beeson
Oct 4 '17 at 16:46
I wish this was the accepted answer. I passed this up initially, then 20 minutes later found a blog post that pointed back here. Could have saved me some time.
– Scott Beeson
Oct 4 '17 at 16:46
I wish this was the accepted answer. I passed this up initially, then 20 minutes later found a blog post that pointed back here. Could have saved me some time.
– Scott Beeson
Oct 4 '17 at 16:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11042212%2fff-13-ie-9-json-stringify-geolocation-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown