hedera-web/js/vn/url.js

77 lines
1.3 KiB
JavaScript

/**
* Class to handle the URL.
**/
Vn.Url =
{
/**
* Gets the value of a URL variable.
*
* @param {string} key The variable name
**/
getQuery: function (key)
{
var regExp = new RegExp ('[\?\&]'+ key +'=([^\&]*)(\&?)', 'i');
var value = location.search.match (regExp);
return value ? value[1] : value;
}
/**
* Sets the value of a URL variable.
*
* @param {string} key The variable name
* @param {string} value The new value
**/
,setQuery: function (key, value)
{
var changed = true;
var found = false;
var newPair = key +'='+ value;
var kvPairs = location.search.substr(1).split ('?');
for (var i = 0; i < kvPairs.length; i++)
{
var kvPair = kvPairs[i].split ('=', 1);
if (kvPair[0] == key)
{
if (kvPair[1] != value)
kvPairs.splice (i, 1, newPair);
else
changed = false;
found = true;
break;
}
}
if (!found)
kvPairs.push (newPair);
if (changed)
document.location.hash = '?'+ kvPairs.join ('&');
}
,makeUri: function (map)
{
var post = '';
for (var key in map)
{
var value = map[key];
if (post.length > 2)
post += '&';
if (value === null || value === undefined)
continue;
if (typeof value == 'boolean')
value = value ? '1' : '0';
post += key +'='+ encodeURIComponent (value);
}
return post;
}
};