mr-crocs-adventures/js/UrlParam.js

59 lines
1.1 KiB
JavaScript

export default class UrlParam
{
constructor()
{
this.url = document.location.toString();
console.log(this.url);
this.params = UrlParam.getParamsFromUrl(this.url);
}
get(name)
{
return this.params[name];
}
static getParamsFromUrl(url)
{
let urlParts = url.split('?');
if (urlParts.length !== 2) {
return [];
}
let params = urlParts[1].split('&');
if (params.length === 0) {
return [];
}
let pairs = [];
params.forEach(
(param) => {
let parts = param.split('=');
if (parts.length !== 2) {
return;
}
pairs[parts[0]] = parts[1];
}
);
return pairs;
}
getInt(name)
{
let value = parseInt(this.get(name));
return isNaN(value) ? undefined : value;
}
getFloat(name)
{
let value = parseFloat(this.get(name));
return isNaN(value) ? undefined : value;
}
}