user6599525 user6599525 - 1 year ago 59
Javascript Question

How to get value of query string from encoded url using javascript?

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
from that ?

Answer Source

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);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download