1

I'm developing a web analytics for my app.

Client side, I send to the server the window.location object over a JSON.stringify(window.location).

The object is properly stringified in chrome, IE, opera... but in Firefox, it just return {"constructor":{}}.

What's happen with Firefox ?

4 Answers 4

2

I don't know why it doesn't work under FireFox (I assume it has something to do with the fact that properties of window.location are actually getters/setters in FireFox), but here's a simple solution: just copy the object.

var copy = {};
for (var i in window.location) {
    copy[i] = window.location[i];
}
JSON.stringify(copy);
Sign up to request clarification or add additional context in comments.

1 Comment

It has to do with the properties not being own properties of the object but rather properties on the prototype, which is what the spec requires...
1

Alternately, can't you simply create a new object that isn't the Location API, but just a simple KVP object?

JSON.stringify({
    href:     window.location.href,
    protocol: window.location.protocol,
    host:     window.location.host,
    hostname: window.location.hostname,
    port:     window.location.port,
    pathname: window.location.pathname,
    search:   window.location.search,
    hash:     window.location.hash,
    username: window.location.username,
    password: window.location.password,
    origin:   window.location.origin
});

Comments

0

window.location is an instance of Location interface. You probably want to get its url as string. Try window.location.href instead.

3 Comments

@freakish, I'm pretty sure OP wants to send URL of a current page.
Where does OP state that? There's even more: he clearly states that "The object is properly stringified in chrome", so he actually want the object jsonified.
You can also jsonify a string. And href contains all the information from the object. It is not conveniently parsed though.
0

Freakish solution does work.

var window_location = {};
for (var i in window.location) {
    window_location[i] = window.location[i];
}
alert(JSON.stringify(window_location));

See this in action here...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.