Reputation: 106380
Is there a reasonable way to detect or infer the visitor's base currency using Javascript?
I'm thinking of using IP geolocation and timezone detection. Do you know of any existing libraries / methods for this, or alternatives?
Upvotes: 1
Views: 820
Reputation: 21476
There is no way to do it purely with JavaScript that I know of.
I think the easiest way is to invoke a server-side script which determines currency based on the Accept-Language header, using your scripting-langauge of choice's localization functions. Using Accept-Language for anything more than language is debatable though. Personally I think it's fine, as long as you provide the visitor with a way to switch currencies.
IP geolocation may also be a viable alternative, but I'm not entirely sure how accurate this would be throughout the world. Again, just be sure to provide the visitor with a way to switch currencies.
Upvotes: 1
Reputation: 32537
afaik there is no way to get the user's IP or any other "geolocation" information with javascript. However, you can get their local timezone offset by using .getTimezoneOffset()
Upvotes: 1