Frank Danny
asked on
How can I extract Id from a URL using Javascript?
Can anyone please help? I am trying to extract an ID 254162 from URL http://localhost:9080/company_add.jsp?id=254162. I have tried to write as seen below but I when I do console.log,Java script return /company_add.jsp only.Anything I am missing?.Thank you
var url=window.location.pathname;
var UrId=url.substring(url.lastIndexOf('=') + 1);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The regular expression works by looking at 3 parts of the URL:
1. the part up to and including id=
2. a number
3. everything after the number, if anything.
The replace command replaces the lot with just the value from 2. above. This is done by putting brackets around the part of the pattern used to match the number, which captures it as $1. It can then be used as the replacement for the whole string.
1. the part up to and including id=
2. a number
3. everything after the number, if anything.
The replace command replaces the lot with just the value from 2. above. This is done by putting brackets around the part of the pattern used to match the number, which captures it as $1. It can then be used as the replacement for the whole string.
Another alternative not using Regex - gets the parameters into an Object
<script>
var inp = ' http://localhost:9080/company_add.jsp?id=254162&a=fred&b=donky&c=adsf09afua0ufa';
function getUrlParams(url)
{
var temp = url.split('?');
var result = {}
if (temp[1]) {
var params = temp[1].split('&');
for(var i = 0; i < params.length; i++) {
var t = params[i].split('=');
result[t[0]] = t[1]
}
}
return result;
}
var params = getUrlParams(inp);
if (params.id) console.log(params.id);
</script>
ASKER
Thank you@Julian, @Terry and @Hielo.I did decide to go with Terry solutions but facing issue with regex expression.I have tried to test regex expression on regex101.com, with no issue.Am I missing anything here(i.e I did try to type on keyboard as well instead of copy-paste)?below is the error under F12 tools on internet explorer v11
ASKER
One thing: I see
var url=window.location.pathname
is returned as "/company_view.jsp" after I did console.log.I want to return a complete current url which is http://localhost:9080/company_add.jsp?id=254162
ASKER
Now I was able to return the URL by
var url=window.location.href
; but the regex failed with the same error
The solution you are using does a replace - I thought you wanted to do an extract which would require a match
<script>
var url = window.location.href;
var result = url.match(/id=(\d+)/);
var id = result ? result[1] : false;
console.log(result);
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you@Julian...it works :)
You are welcome.
Open in new window
Output:Open in new window