QueryString using JQuery
There are lots of ways to get query-string from the web URL by using jQuery but this is the best way I feel to retrieve one or more than one query-strings parameters from the URL.
How to do it?
Let's suppose your URL is http://YourUrl.aspx.
It has 2 query-string parameters i.e. "itemid" and "status".
The parameter "itemid" has value as "1" and parameter "status" has value as "Approve".
So, now your URL will be http://YourUrl.aspx?itemid=1&status=Approve.
In your (document).ready() function, add the code as shown below for the URL which has two query-string parameters (i.e. http://YourUrl.aspx?itemid=1&status=Approve):
$(document).ready(function () {
var queryString = new Array();
if (queryString.length == 0) {
if (window.location.search.split('?').length > 1) {
var params = window.location.search.split('?')[1].split('&');
for (var i = 0; i < params.length; i++) {
var key = params[i].split('=')[0];
var value = decodeURIComponent(params[i].split('=')[1]);
queryString[key] = value;
}
}
}
if (queryString["itemid"] != null && queryString["status"] != null) {
try {
alert("Your Item ID is: " + queryString["itemid"] + ", Your Status is:" + queryString["status"]);
}
catch (e) { }
}
});
As you can see that using array variable, we can store more than one query-string parameters and retrieve them by passing their key.
I hope this helps!!
No comments:
Post a Comment