Get escaped URL parameter
You should not use jQuery for something like this!
The modern way is to use small reusable modules through a package-manager like Bower.
I've created a tiny module that can parse the query string into an object. Use it like this:
// parse the query string into an object and get the property
queryString.parse(unescape(location.search)).search;
//=> æøå
How to encode URL parameters?
With PHP
echo urlencode("http://www.image.com/?username=unknown&password=unknown");
Result
http%3A%2F%2Fwww.image.com%2F%3Fusername%3Dunknown%26password%3Dunknown
With Javascript:
var myUrl = "http://www.image.com/?username=unknown&password=unknown";
var encodedURL= "http://www.foobar.com/foo?imageurl=" + encodeURIComponent(myUrl);
DEMO: http://jsfiddle.net/Lpv53/
Escaping ampersand in URL
They need to be percent-encoded:
> encodeURIComponent('&')
"%26"
So in your case, the URL would look like:
http://www.mysite.com?candy_name=M%26M
How to get a parameter from a URL encoded?
The encoding is happening in your first line already:
string queryString = new Uri(URL).Query;
Presumably you want to avoid writing your own code to extract the Query part from a URL. Which is sensible. You can still rely on the Uri
class to do the parsing for you:
var uri=new Uri(URL);
var queryString= URL.Replace(uri.GetLeftPart(UriPartial.Path), "").TrimStart('?');
———————————————————————————————————
(
var URL="http://a/b/?d= @£$%% sdf we 456 7 5?367";
var uri=new Uri(URL);
Console.WriteLine(uri.Query); // Already url-encoded by the Uri constructor
var queryString = URL.Replace(uri.GetLeftPart(UriPartial.Path), "").TrimStart('?');
Console.WriteLine(System.Web.HttpUtility.ParseQueryString(queryString)); //Not encoded!
)
(As an aside, LinqPad helps)
Non-escaped query parameters for URL
If you don't want your URL query to be encoded, then don't use the Encode()
method. Instead, set the RawQuery
value directly, yourself:
url, _ := url.Parse("https://android-review.googlesource.com/changes/")
url.RawQuery = "q=before:{2019-12-24 19:57:34}"
fmt.Println(url)
Output:
https://android-review.googlesource.com/changes/?q=before:{2019-12-24 19:57:34}
playground
Keep in mind, however, that this is a recipe for potential disaster, depending on how that url is eventually used. In particular, the space in that URL should be escaped, according to RFC. See more here.
Perhaps you'll want to implement your own minimal escaping, if that's compatible with your use-case.
slashes in url variables
You need to escape the slashes as %2F
.
Encode URL in JavaScript?
Check out the built-in function encodeURIComponent(str) and encodeURI(str).
In your case, this should work:
var myOtherUrl =
"http://example.com/index.html?url=" + encodeURIComponent(myUrl);
How can I correctly encode query parameters in a url?
When dealing with HttpClient related tasks, A better approch is to use some handly library like RestSharp.
Using RestSharp NuGet library (https://www.nuget.org/packages/RestSharp) - You don't need to worry about URL encoding, JSON parsing and a lot more
var client = new RestClient("https://countries.com");
var request = new RestRequest("GetCountry", Method.GET);
// As you mentioned, If you do no need to encode URL parameters. Add the encoding off option
request.AddQueryParameter("countryName", "India", ParameterType.QueryStringWithoutEncode);
request.AddQueryParameter("cityName", "Kochi", ParameterType.QueryStringWithoutEncode);
var response = client.Execute(request);
This will give
https://countries.com/GetCountry?countryName=India&cityName=Kochi
URL encoding is automaticaly handled. You don't need to worry about it
Related Topics
What Do Multiple Arrow Functions Mean in JavaScript
Using the Variable "Name" Doesn't Work With a Js Object
JavaScript Set Object Key by Variable
Chrome/Firefox Console.Log Always Appends a Line Saying 'Undefined'
How to Deep Merge Instead of Shallow Merge
Calling Functions With Settimeout()
When Should I Use Curly Braces For Es6 Import
Create and Save a File With JavaScript
Why Is Setstate in Reactjs Async Instead of Sync
How to Test For an Empty JavaScript Object
How to Insert an Item into an Array At a Specific Index (JavaScript)
How to Empty an Array in JavaScript
Listening For Variable Changes in JavaScript
How to Determine the Direction of a Jquery Scroll Event