Hello guys,
I have this javascript code that I want to standardize or I want to recode to make it more standard. I have also a basis on how to to this. But since I am new with Javascript, I really need your help.
The javascript that I need to standardize:
function updateQS(sName, sVal, blnPost)
{
var qs = self.location.search;
if (qs == '')
{
qs = "?" + sName + "=" + escape(sVal);
}
else
{
if (qs.indexOf(sName) == -1)
{
qs = qs + "&" + sName + "=" + escape(sVal);
}
else
{
qs = eval('qs.replace(/(\\\\?|\\\\&)' + sName + '=([^\\\\&]*)/, "$1' + sName + '=' + escape(sVal) + '")');
}
}
if (blnPost)
{
self.location.replace("ProjectSearch.asp" + qs);
}
}
My basis, this is already standard:
function updateSort(pageURL, newSortCol, currentSortCol, currentSortOrder)
{
var newSortOrder
//switch ascending to descending (and vice-versa)
if (currentSortOrder == 'ASC')
{
newSortOrder = 'DESC';
}
else
{
newSortOrder = 'ASC';
}
//sortCol is in the querystring, so we have to replace the value of it and determine what sort order to use
if (newSortCol != currentSortCol)
{
//new sort column so force ascending
newSortOrder = 'ASC';
}
//this function alters the URL's querystring for sorting purposes and then refreshes the page (using location.replace)
var qs = self.location.search;
if (qs == '')
{
//if the current querystring is blank then just add the sorting column and sorting order
qs = '?sortCol=' + newSortCol + '&sortOrder=' + newSortOrder;
}
else
{
//if the current querystring is non-blank we have to replace the existing sorting column and sorting order (if any)
if (qs.indexOf('sortCol') == -1)
{
//sortCol is not in the querystring, so you can append it to the end as a new variable and assume order is ascending
qs = qs + '&sortCol=' + newSortCol + '&sortOrder=' + newSortOrder;
}
else
{
//replace the value of sortCol
qs = eval('qs.replace(/(\\\\?|\\\\&)' + 'sortCol' + '=([^\\\\&]*)/, "$1' + 'sortCol' + '=' + newSortCol + '")');
//replace the value of sortOrder
qs = eval('qs.replace(/(\\\\?|\\\\&)' + 'sortOrder' + '=([^\\\\&]*)/, "$1' + 'sortOrder' + '=' + newSortOrder + '")');
}
}
//2...replace the location
self.location.replace(pageURL + qs);
}
Problem: Make the updateQS function as standard as updateSort function.
Any advised, codes will be appreciated, thanks in advanced.