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.

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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";
    }

Comments

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

1 Comment

If you're going to copy this, link to it. The comments are useful: stackoverflow.com/questions/901115/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.