jQuery - Querystring
This code was created to make easier to get and to treat query strings. It may return the value of one query string, a collection of query strings, the length, select query string by its index and turn off the case sensitivity.
Suggested amount $25
Installing
Add the following files to your website
// If you don't have the jQuery file already informed, insert the code bellow
<script type="text/javascript" src="javascript/jquery-1.4.2.min.js"></script>
// Insert the following code after the jQuery code in your website if you prefer
// the compact version or ...
<script type="text/javascript" src="source/querystring-0.9.0-min.js"></script>
// ... insert the following if you prefer the development version
<script type="text/javascript" src="source/querystring-0.9.0.js"></script>
Usage
If not informed, $.QueryString will get the browser's address bar URL. To inform a different one, check the options parameters.
// Return null if there is no query string
$.QueryString();
// Return the length of query strings
$.QueryString().size;
// Return null is the query string doesn't exist or the value of the
// query string if it exists
$.QueryString( //string );
// Get the length of query strings with the same name
$.QueryString( //string ).length;
// Return que value of a query string modifying the URL, the choosing
// its index and turning on or off the case sensitivity
$.QueryString( //string, { // options
href : // URL string,
index : // number,
isCaseSensitive : // boolean
});
// Get the value of a query string as an object
$.QueryString().objectName;
The options paremeters and its values are
- href
- (string)(optional) URL with or without query strings where the query string wanted will be searched.
- Default: window.location
- index
- (number)(optional) It returns the value of a query string in a collection of query strings with same name.
- Default: null
- isCaseSensitive
- (boolean)(optional) Decides if the query string searched will have the case sensitivity considered.
- Default: true
Examples
To start you will need query strings in your address bar for test. If they don't appear automatically, clique here.
// Get null for "country". This query string is not in the address bar
$("#country").click(function(){
var message = $.QueryString("country"),
message = ( !message )? "null":message; // Passing the value null to string
$("#msg_country").html( message );
});
// Get "language" query string value from the address bar
$("#language").click(function(){
var message = $.QueryString("language"),
message = ( !message )? "null":message; // Passing the value null to string
$("#msg_language").html( message );
});
// Gets the length of query strings from the address bar
$("#length").click(function(){
var message = $.QueryString().size,
message = ( !message )? "null":message; // Passing the value null to string
$("#msg_length").html( message );
});
// Gets "language" query string as object
$("#languageAsObject").click(function(){
var message = $.QueryString().language,
message = ( !message )? "null":message; // Passing the value null to string
$("#msg_languageAsObject").html( message );
});
// Gets the length of "province" query strings from the address bar
$("#provinceLength").click(function(){
var message = $.QueryString("province"),
message = ( $.isArray( message ) )? message.length:"null"; // Passing the value null to string;
$("#msg_provinceLength").html( message );
});
// Gets "province" query string value from the address bar using a index
$("#provinceByIndex").click(function(){
var message = $.QueryString("province", { index : 2 }),
message = ( !message )? "null":message; // Passing the value null to string;
$("#msg_provinceByIndex").html( message );
});
// Gets "province" query string value from the address bar using a index and non-case sensitivity
$("#provinceNonCaseSensitivity").click(function(){
var message = $.QueryString("Province", {
index : 2,
isCaseSensitive : false
}),
message = ( !message )? "null":message; // Passing the value null to string;
$("#msg_provinceNonCaseSensitivity").html( message );
});
// Gets "country" query string value from a different url
$("#informingURL").click(function(){
var message = $.QueryString("country", {
href : "http://www.darlesson.com/?country=Brazil"
}),
message = ( !message )? "null":message; // Passing the value null to string;
$("#msg_informingURL").html( message );
});
