I know that this question has been asked more than once here. But the issue I'm facing is a bit different that the one's I checked in previous questions.
I'm retrieving the Category in a variable from the URL in my JS by doing this-
$scope.category = url.split("=")[1]
This would be a sample URL-
sitename/pagename.aspx?Category=Cars
I'm facing an issue when there is a space in the category. Say if the URL is like this-
sitename/pagename.aspx?Category=Super%20Cars
Can you please lemme know how to replace the %20 by a space along with the url.split.
Sure this isnt a right one-
$scope.category = decodeURIComponent(url.split("=")[1]);
Lemme know if you need more info guys. :)
I know that this question has been asked more than once here. But the issue I'm facing is a bit different that the one's I checked in previous questions.
I'm retrieving the Category in a variable from the URL in my JS by doing this-
$scope.category = url.split("=")[1]
This would be a sample URL-
sitename/pagename.aspx?Category=Cars
I'm facing an issue when there is a space in the category. Say if the URL is like this-
sitename/pagename.aspx?Category=Super%20Cars
Can you please lemme know how to replace the %20 by a space along with the url.split.
Sure this isnt a right one-
$scope.category = decodeURIComponent(url.split("=")[1]);
Lemme know if you need more info guys. :)
Share Improve this question edited Aug 23, 2016 at 4:37 theLearner asked Aug 23, 2016 at 4:28 theLearnertheLearner 3715 silver badges18 bronze badges 5-
you mean
decodeURIComponent(url.split("=")[1]);
? – Jaromanda X Commented Aug 23, 2016 at 4:30 - yes, typed it wrong :) – theLearner Commented Aug 23, 2016 at 4:35
- Are you not able to replace directly like var txt = url.replace("%20"," "); ? – Vickyexpert Commented Aug 23, 2016 at 4:41
- Gosh, I dunno how I missed it..:D:D..Its working ..Thanks man..:D – theLearner Commented Aug 23, 2016 at 4:44
- Enjoy your coding, you can accept answer below – Vickyexpert Commented Aug 23, 2016 at 4:47
5 Answers
Reset to default 3You can try this:
url.split('%20').join(' ');
You can do it by below syntax:
var url = window.location.href;
var tempUrl = url.replace(/%20/g," ");
$scope.category = tempUrl.split("=")[1]
You need to use decodeURI
to achieve this.
http://www.w3schools./jsref/jsref_decodeuri.asp
Use decodeURI
global function available in javascript. http://www.w3schools./jsref/jsref_decodeuri.asp
var url="http://www.dummyserver./index.php?param1=hello%20world";
var queryParams=url.substring(url.indexOf("?") + 1).split("&");
for(i=0;i<queryParams.length;i++){
var splitParam = queryParams[i].split("=");
alert("Param: " + splitParam[0] + " :: Value:" + decodeURI(splitParam[1]));
}
You can convert the url into string first and then replace %20 with the space and then split,it should work,can try like this:
var str = 'sitename/pagename.aspx?Category=Super%20Cars';
var res = str.toString();
res = res.replace('%20',' ');
category = decodeURIComponent(res.split("=")[1]);