I have the following encoded URL string in javascript
var querystring = "http%3A%2F%2Fspsrv%3A1361%2FSitePages%2FCountryManagment%2Easpx%3FCountryId%3D1";
How can I get the value of CountryId from that ?
You will need to decode the URL with decodeURIcomponent and then use regex to get the parameter from URL
var querystring = decodeURIComponent("http%3A%2F%2Fspsrv%3A1361%2FSitePages%2FCountryManagment%2Easpx%3FCountryId%3D1");
document.getElementById("decodedURL").innerHTML = querystring;
function getParam(param, url) {
if (!url) url = window.location.href;
param = param.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + param + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return results[2].replace(/\+/g, " ");
}
document.getElementById("parameter").innerHTML = getParam("CountryId", querystring);
Demo: JSFIDDLE
At first, you should decode url, using .decodeURI() you can do it. Then you can use .split() to spliting URL to array or use .match() to selecting specific part of URL by regex.
var querystring = "http%3A%2F%2Fspsrv%3A1361%2FSitePages%2FCountryManagment%2Easpx%3FCountryId%3D1";
var result = decodeURIComponent(querystring).split("?")[1].split("=")[1];
var result2 = decodeURIComponent(querystring).match(/CountryId=([\d]+)/)[1];
console.log(result, result2);
CountryId?