In this article I will explain with an example, how to read
QueryString parameters in
JavaScript.
There is no direct function or utility available to read QueryString parameter values in
JavaScript.
This article will illustrate how to parse the URL of the page and then extract the QueryString parameter values using Plain
JavaScript.
Redirecting with QueryString parameters using JavaScript
The following HTML Markup consists of an HTML TextBox, a DropDownList and a Button.
The Button has been assigned a
JavaScript Click event handler which invokes the
Send JavaScript function.
Inside the Send
JavaScript function, the values of the TextBox and DropDownList are fetched and are encoded using the
JavaScript encodeURIComponent function and added as QueryString parameters to a URL.
Finally, the page is redirected to the URL.
Name:
<input type="text" id="txtName" name="Name" value="Mudassar Khan" /><br />
<br />
Technology:
<select id="ddlTechnolgy" name="Technology">
<option value="ASP.Net">ASP.Net</option>
<option value="PHP">PHP</option>
<option value="JSP">JSP</option>
</select>
<input type="button" value="Send" onclick="Send()" />
<script type="text/javascript">
function Send() {
var name = document.getElementById("txtName").value;
var tech = document.getElementById("ddlTechnolgy").value;
var url = "Page2.htm?name=" + encodeURIComponent(name) + "&technology=" + encodeURIComponent(tech);
window.location.href = url;
};
</script>
Reading QueryString parameters using JavaScript
The following HTML Markup consists of an HTML SPAN.
Inside the GetQueryString function, the QueryString is extracted by splitting the URL of the current page using the Question mark (?) character.
Then a loop is executed and each QueryString parameter value is extracted by further splitting using the ampersand (&) character and the extracted values are decoded using the
JavaScript decodeURIComponent function inserted as Key and Value pairs into an Array.
Finally, the QueryString parameter values are fetched from the Array using the name of the QueryString parameter i.e. the Key and displayed in HTML SPAN.
<span id="lblData"></span>
<script type="text/javascript">
window.onload = function () {
var queryString = GetQueryString();
if (queryString["name"] != null && queryString["technology"] != null) {
var data = "<u>Values from QueryString</u><br /><br />";
data += "<b>Name:</b> " + queryString["name"] + " <b>Technology:</b> " + queryString["technology"];
document.getElementById("lblData").innerHTML = data;
}
};
function GetQueryString() {
var queryString = new Array();
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;
}
}
return queryString;
};
</script>
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Demo
Downloads