2009/08/28 08:32
Javascript based query parser for AJAX application
2009/08/28 08:32 in HTML, Javascript

When you want to pass data from one web page to another when server-side scripting is not available, and without setting a cookie? Here's the really good javascript you can use.
<script>
function QueryParser(str)
{
if (str)
{
str = unescape(str);
if (str.indexOf("?") === 0)
{
str = str.substring(1);
}
var args = str.split("&");
for (var i = 0; i < args.length; i++)
{
var pair = args[i].split("=");
if (pair.length >= 1)
{
var prop = pair.shift();
this[prop] = (pair.length == 1) ? pair[0] : (pair.length > 1) ? pair.join('=') : '';
}
}
}
this.set = function (prop, value) { return this[prop] = value; };
this.clear = function (prop)
{
if(typeof this[prop] !== 'undefined')
{
this.set(prop, null);
return true;
}
else { return false; }
};
this.build = function (baseURL, hashName)
{
baseURL = (!baseURL || typeof baseURL !== 'string') ? '?' : (baseURL.indexOf("?") === -1) ? (baseURL + '?') : baseURL;
hashName = (!hashName || typeof hashName !== 'string') ? '' : (hashName.indexOf("#") === -1) ? ('#' + hashName) :
hashName;
var strArray = [];
for (var prop in this)
{
if (typeof this[prop] !== 'undefined' && typeof this[prop] !== 'function' && this[prop] !== null)
{
strArray.push([prop, '=', this[prop]].join(''));
}
}
return baseURL + strArray.join('&') + hashName;
};
this.buildLink = function (baseURL, linkTxt)
{
var url = this.build(baseURL);
return [ '<a href="', url, '">', ((!linkTxt) ? url : linkTxt), '</a>' ].join('');
};
}
var $q = new QueryParser(location.search);
</script>
function QueryParser(str)
{
if (str)
{
str = unescape(str);
if (str.indexOf("?") === 0)
{
str = str.substring(1);
}
var args = str.split("&");
for (var i = 0; i < args.length; i++)
{
var pair = args[i].split("=");
if (pair.length >= 1)
{
var prop = pair.shift();
this[prop] = (pair.length == 1) ? pair[0] : (pair.length > 1) ? pair.join('=') : '';
}
}
}
this.set = function (prop, value) { return this[prop] = value; };
this.clear = function (prop)
{
if(typeof this[prop] !== 'undefined')
{
this.set(prop, null);
return true;
}
else { return false; }
};
this.build = function (baseURL, hashName)
{
baseURL = (!baseURL || typeof baseURL !== 'string') ? '?' : (baseURL.indexOf("?") === -1) ? (baseURL + '?') : baseURL;
hashName = (!hashName || typeof hashName !== 'string') ? '' : (hashName.indexOf("#") === -1) ? ('#' + hashName) :
hashName;
var strArray = [];
for (var prop in this)
{
if (typeof this[prop] !== 'undefined' && typeof this[prop] !== 'function' && this[prop] !== null)
{
strArray.push([prop, '=', this[prop]].join(''));
}
}
return baseURL + strArray.join('&') + hashName;
};
this.buildLink = function (baseURL, linkTxt)
{
var url = this.build(baseURL);
return [ '<a href="', url, '">', ((!linkTxt) ? url : linkTxt), '</a>' ].join('');
};
}
var $q = new QueryParser(location.search);
</script>
Following is the simple example for above javascript.
EXAMPLE
URL: http://www.mydomain.com/mydir/page.h...&potential=110
$q.name will hold the value 'Super Coder'
$q.rank will hold the value '9999999999'
$q.potential will hold the value '110'
Another posts included in "HTML, Javascript"
| Displays the html tags without rendering on a webpage (0) | 2009/08/28 |
| To hide/show area of web page without re-loading a page (0) | 2009/08/28 |
| To compare two HTML tables on web page in real time (0) | 2009/08/28 |
| To get web browser capability (0) | 2009/08/28 |
| Photo thumbnail viewer implement in javascript (0) | 2009/08/28 |
| Javascript Confirmation (0) | 2009/08/22 |
| Javascript Prompts (0) | 2009/08/22 |
| How to know browser version in javascript ? (0) | 2009/08/22 |
Prev

Rss Feed