Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Get Query String Values with Special Characters (&,')

Sep 13, 2013
Introduction

Here I will explain how to get query string parameter value with special characters in
jQuery .

Description:
  
In previous articles I explained jQuery get query string parameter value with spaces,
jQuery get query string parameter values, asp.net pass multiple parameters in query string, jQuery Remove first/last character from string, jQuery create rounded corners for textbox and many articles relating to JQuery. Now I will explain how to get query string parameter value with special characters in jQuery.

To get query string parameter values with special characters we need use escape function in a page while we are passing query string parameter values and we need to use decodeURIComponent JavaScript function in our page where we will retrieve the values  like as shown below

Pass query string values with escape function


<script type="text/javascript">
$(function() {
$('#btnValidate').click(function() {
$(location).attr('href', '/test.aspx?username=' + escape('s & s traders') + '&userid=2');
})
})
</script>
Get Query String Parameter Values with decodeURIComponent


<script type="text/javascript">
$ (function() {
var str = decodeURIComponent(s%20%26%20s%20traders')
alert(str);
})
</script>
If you want check this code in sample check below code

Example:

For first create one sample Default.aspx page and write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Send Query String Parameter values</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#btnValidate').click(function() {
$(location).attr('href', '/test.aspx?username=' + escape('s & s traders') + '&userid=2');
})
})
</script>
</head>
<body>
<div>
<b>Enter URL</b>
<input type="text" id="txturl" />
<input type="button" id="btnValidate" value="Validate" />
</div>
</body>
</html>
After that create another page test.aspx and write the following code and run


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Get Query String Parameter Values with Special Characters</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
var name = GetParameterValues('username');
var id = GetParameterValues('userid');
$('#spn_UserName').html('<strong>' + name + '</strong>');
$('#spn_UserId').html('<strong>' + id + '</strong>');
});
function GetParameterValues(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return decodeURIComponent(urlparam[1]);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>UserName: <span id="spn_UserName"></span></p>
<p>UserId: <span id="spn_UserId"></span></p>
</div>
</form>
</body>
</html>
Demo

To check above example I given ur like “http://aspdotnet-suresh.com/test.aspx?username= s%20%26%20s%20traders &userid=2” and it return values like as shown below

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Unknown said...

Sir,

Please tell me how to post image from image uploader, to save in database using ajax post

please

kishore said...

how to Enabling a Textbox using a checkbox in ASP.NET?

kishore said...

how to copy the data from one dropdownlist to another dropdownlist

how to copy the data from one dropdownlist to aanother dropdown list

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.