user2147163
user2147163

Reputation: 87

Fetch Value of hyperlink in javascript

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.

Upvotes: 0

Views: 107

Answers (4)

Mithun Pattankar
Mithun Pattankar

Reputation: 1372

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

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115282

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

Upvotes: 0

Tomer Keisar
Tomer Keisar

Reputation: 61

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

Upvotes: 0

Julian Fondren
Julian Fondren

Reputation: 5609

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.

Upvotes: 1

Related Questions