0

I have one hyperlink and i need to findout the value of one field. I am sharing the below code

Code:

url="http://localhost/Employee/EmployeeMgmt/EmpDetails.aspx?

EmpId=784657&From=Information Technology";

Now, I need to fetch the value of EmpId=784657.

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
user2147163
  • 87
  • 1
  • 4
  • 13

4 Answers4

1
var url="http://localhost/Employee/EmployeeMgmt/EmpDetails.aspx?EmpId=784657&From=Information Technology",
    empid = /\bEmpId=(\d+)/.exec(url)[1]

empid is the string "784657"

The little \b there guards you against a SimilarEmpId variable. decodeURI and may also be useful to you.

Julian Fondren
  • 5,459
  • 17
  • 29
0

You can use match() with regex \bEmpId=(\d+)&. You can get the capturing group value from result using index 1

var url = "http://localhost/Employee/EmployeeMgmt/EmpDetails.aspx?EmpId=784657&From=Information Technology";

console.log(url.match(/\bEmpId=(\d+)&/i)[1])
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Its actually getting query string of url. Try this pure javascript code

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

To retrieve particular query string

var empId = getParameterByName('EmpId');

Check this Getting Query String

Community
  • 1
  • 1
Mithun Pattankar
  • 1,372
  • 1
  • 8
  • 12
  • If you're going to copy this, link to it. The comments are useful: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Julian Fondren Oct 12 '15 at 18:22
0

please try:

 function search(url,param) {
        var params = url.split("?")[1].split("&");
        for (i in params) {
            var key = params[i].split("=")[0];
            if (key == param) {
                return params[i].split("=")[1];
            }
        }
        return "not found";
    }