Reputation: 87
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
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
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
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
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