Skip to content

Commit 49bac5d

Browse files
committed
Added a method to enableSessionStorage to store API results in the client's sessionStorage to avoid duplicate calls on multiple pages
1 parent d01df4a commit 49bac5d

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

ipgeolocation.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var ess = false;
12
var asyncParameter = true;
23
var ipAddressParameter = "";
34
var excludesParameter = "";
@@ -7,40 +8,44 @@ var tzParameter = "";
78
var latitudeParameter = "";
89
var longitudeParameter = "";
910

11+
function enableSessionStorage(enable = false) {
12+
ess = enable;
13+
}
14+
1015
function setAsync(async = true) {
1116
asyncParameter = async;
1217
}
1318

14-
function setIPAddressParameter (ip = "") {
19+
function setIPAddressParameter(ip = "") {
1520
ipAddressParameter = ip;
1621
}
1722

18-
function setExcludesParameter (excludes = "") {
23+
function setExcludesParameter(excludes = "") {
1924
excludesParameter = excludes;
2025
}
2126

22-
function setFieldsParameter (fields = "") {
27+
function setFieldsParameter(fields = "") {
2328
fieldsParameter = fields;
2429
}
2530

26-
function setLanguageParameter (lang = "") {
31+
function setLanguageParameter(lang = "") {
2732
langParameter = lang;
2833
}
2934

30-
function setTimezoneParameter (tz = "") {
35+
function setTimezoneParameter(tz = "") {
3136
tzParameter = tz;
3237
}
3338

34-
function setCoordinatesParameter (latitude = "", longitude = "") {
39+
function setCoordinatesParameter(latitude = "", longitude = "") {
3540
latitudeParameter = latitude;
3641
longitudeParameter = longitude;
3742
}
3843

39-
function getGeolocation (callback, apiKey = "") {
44+
function getGeolocation(callback, apiKey = "") {
4045
request("ipgeo", callback, apiKey);
4146
}
4247

43-
function getTimezone (callback, apikey = "") {
48+
function getTimezone(callback, apikey = "") {
4449
request("timezone", callback, apikey);
4550
}
4651

@@ -50,13 +55,22 @@ function addUrlParameter(parameters, parameterName, parameterValue) {
5055
} else {
5156
parameters = "?".concat(parameterName, "=", parameterValue);
5257
}
58+
5359
return parameters;
5460
}
5561

56-
function request (subUrl, callback, apiKey = "") {
62+
function request(subUrl, callback, apiKey = "") {
63+
if (ess) {
64+
if (subUrl == "ipgeo" && sessionStorage.getItem("_ipGeolocation") && callback) {
65+
callback(JSON.parse(sessionStorage.getItem("_ipGeolocation")));
66+
} else if (subUrl == "timezone" && sessionStorage.getItem("_ipTimeZone") && callback) {
67+
callback(JSON.parse(sessionStorage.getItem("_ipTimeZone")));
68+
}
69+
}
70+
5771
var urlParameters = "";
5872

59-
if(!subUrl) {
73+
if (!subUrl) {
6074
callback(JSON.parse("{'status': 401, message: 'Given path to IP Geolocation API is not valid'}"));
6175
return;
6276
}
@@ -97,6 +111,14 @@ function request (subUrl, callback, apiKey = "") {
97111
contentType: "application/json",
98112
dataType: "json",
99113
success: function (result, status, xhr) {
114+
if (ess) {
115+
if (subUrl == "ipgeo") {
116+
sessionStorage.setItem("_ipGeolocation", JSON.stringify(result));
117+
} else if (subUrl == "timezone") {
118+
sessionStorage.setItem("_ipTimeZone", JSON.stringify(result));
119+
}
120+
}
121+
100122
if (callback) {
101123
callback(result);
102124
}

0 commit comments

Comments
 (0)