A query string is an optional part of a URL that goes after the file name and begins with a question mark (?). For example, the following URL has a query string ?quryEg after the HTML file name:
http://www.findnerd.com/file.html?quryEg
Here is an example code to get the query string values:-
<script type="text/javascript">
function getquryvalues (querystring) {
name = querystring.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if (results == null) {
return "";
}
else {
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
}
var userid = getquryvalues('userid');
var username = getquryvalues('username');
</script>
In the above code regex.exec test for a match in the string and decodeURIComponent is used to decode the URI component.
This is how you can use this code to get the query string value using javascript.
0 Comment(s)